英文:
How to check if a file retrieved from the ftp site exists in the local folder in java using talend?
问题
我正在使用 FtpGet 从 FTP 中提取或检索文件,然后将其加载到数据库中,之前我会将其存储在本地文件夹中。
因此,在使用 tfilecopy 将文件复制到本地文件夹之前,我希望进行一个检查,如果文件已经存在于本地文件夹中,则跳过或忽略接下来的步骤;如果文件不存在,则执行写入操作(tfilecopy)到本地文件夹中。
基本上,我想要遍历本地文件夹中文件的列表,根据我正在提取的文件,在全局映射变量动态地检查该文件是否在文件列表中存在,然后执行操作。
我在 Talend 中创建了这个过程,我可以查询数据库以查看文件是否存在,或者直接检查本地文件夹,看是否已经有了 FTP 文件的副本(如果相同的文件已经存在),然后跳过流程。
只有当文件不存在时,才会写入本地文件夹。
哪种方法是最佳的,是扫描内部文件夹(尽管可能存在子文件夹)并编写 tjava 代码进行检查,
还是使用数据库脚本查询,仅当表中不存在该文件名时,将该文件复制到目标文件夹并写入数据库表。
所以主要是通过 tfilelist3 中的当前文件进行迭代,并使用 TJAVA 检查它们是否存在于 tfilelist2 组件中,仅当从 tfilelist_3 检索的文件在 tfilelist_2 中不存在时,执行 tfilecopy。
TJAVA 代码如下:
String path = ((String) globalMap.get("tFileList_2_CURRENT_FILEDIRECTORY"));
System.out.println("PRINTING PATH in string: " + path);
Path filepath = Paths.get(path);
System.out.println("PRINTING PATH in PATH: " + filepath);
String fileName = ((String) globalMap.get("tFileList_3_CURRENT_FILE"));
System.out.println("PRINTING FILENAME IN STRING: " + fileName);
File file = new File(fileName);
System.out.println("File is " + file);
if (file.exists()) {
System.out.println("Filename: " + file);
System.out.println("Exist in location!");
} else {
System.out.println("Filename: " + file);
System.out.println("Does not exist in location!");
}
我可能使它变得有点复杂,现在我有点迷失,如果有人能够解决剩下的部分并使其工作,那将是很好的,我似乎找不到通过这个迷宫的方法了!我尝试了不同的版本,似乎没有一个能够产生我寻找的结果?
因此,file.exists() 只检查当前目录中的特定文件,如果我需要检查不同目录中的文件,我该如何传递参数?
方法 2:
英文:
I am using FtpGet to extract or retrieve a file from the ftp and loading into the database and before that i am storing in a local folder.
So before i use tfilecopy to the local Folder i would like to perform a check wherein if the file already exists in the local folder skip or ignore the next steps,if they dont exist then only write (tfilecopy) to the local folder.
So basically i want to iterate through the list of files in the local folder based on the one i am retrieving using
GlobalMap variables:dynamically and check if that file exists or not amongst the list of all the other files in that folder and perform the action.
I Have created this in talend,i could either check the database to see if the file exists or directly check the local folder where there is a copy of the ftp files(if the same file already exists) skip the process.
Only if it does not exist write to the local folder.
Which is the best approach either to scan it in the internal folders(although there maybe sub-folders as well) by writing a tjava code
Or use database script to query and only if the filename does not exist in the table,copy that file onto a target folder and write to a db table.
So primarily i am iterating through the current file from tfilelist3 and checking if they exist in tfilelist2 component using TJAVA and tfilecopy only if the file retrieved does not exist in tfilelist_2
TJAVA:
String path =((String)globalMap.get("tFileList_2_CURRENT_FILEDIRECTORY"));
System.out.println("PRINTING PATH in string: " +path);
Path filepath=Paths.get(path);
System.out.println("PRINTING PATH in PATH: " +filepath);
String fileName =((String)globalMap.get("tFileList_3_CURRENT_FILE"));
System.out.println("PRINTING FILENAME IN STRING: " +fileName);
File file = new File(fileName);
System.out.println("File is " +file);
//File f = new File(path);
if(file.exists())
{
System.out.println("Filename: "+file); //+ path.toString());
System.out.println("Exist in location!");
// System.out.println("File EXIST " +f);
} else
{
System.out.println("Filename: "+file); //+ path.toString());
System.out.println("Does not exist in location!");
}
I kinda made it little complex and i am lost now,would be great if someone could fix the remaining part and make it work,i cant seem to find a way through this maze now !
I had tried different versions and none of the seem to produce the result that i am looking for?
So file.exists() only checks for the specific file in the current directory,so what if i need to check in a different directory,how do i pass the arguments?
答案1
得分: 2
你可以创建一个Java例程,例如:
findFile("fileName", "FilePath")
通过将fileName
和filePath
作为参数,为每个文件调用该例程。
英文:
You can create a java routine eg:
findFile("fileName","FilePath")
Invoke the routine for each file by passing the fileName
and filePath
as parameters.
答案2
得分: 1
你有检查过 tFileExist 组件吗?看起来它可能会很有用(唯一的参数是你想要检查的文件夹+文件名)。
英文:
Did you check tFileExist component ? It seems that it could be useful (only parameter is folder+filename you want to check ).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论