Java 将所有文件移动到unix后写入触发器文件
我想将所有文件移动到unix,一旦所有文件都被移动,就向unix写入一个触发器文件。 代码如下:-Java 将所有文件移动到unix后写入触发器文件,java,Java,我想将所有文件移动到unix,一旦所有文件都被移动,就向unix写入一个触发器文件。 代码如下:- public static void download(CloudFileDirectory root, CloudFileDirectory backup) throws StorageException, URISyntaxException, FileNotFoundException { ResultSegment<ListFileItem> list
public static void download(CloudFileDirectory root,
CloudFileDirectory backup) throws StorageException, URISyntaxException,
FileNotFoundException {
ResultSegment<ListFileItem> list =
root.listFilesAndDirectoriesSegmented();
for (ListFileItem item : list.getResults()) {
URI uri = item.getUri();
String path = uri.getPath();
String localPath = localRootDirPath;
String itemName = new File(path).getName();
CloudFile file = root.getFileReference(itemName);
file.download(new FileOutputStream(localRootDirPath+itemName));
CloudFile backupFile = backup.getFileReference(itemName);
backupFile.startCopy(file);
file.delete();
file.download(new
FileOutputStream(localRootDirPath+"ok_business"));
}
}
publicstaticvoid下载(CloudFileDirectory根目录,
CloudFileDirectory备份)引发StorageException、URISyntaxException、,
FileNotFoundException{
结果段列表=
root.listFilesAndDirectoriesSegmented();
对于(ListFileItem项:list.getResults()){
URI=item.getUri();
String path=uri.getPath();
字符串localPath=localRootDirPath;
String itemName=新文件(路径).getName();
CloudFile file=root.getFileReference(itemName);
下载(新的FileOutputStream(localRootDirPath+itemName));
CloudFile backupFile=backup.getFileReference(itemName);
备份文件.startCopy(文件);
delete();
下载(新版本)
FileOutputStream(localRootDirPath+“确定业务”);
}
}
上面的问题是,在第一个文件移动到unix后,它会创建一个触发器文件“ok_business”,但我只想在所有文件都移动后创建该文件。move call
file.download(new
FileOutputStream(localRootDirPath+"ok_business"));
不受你的控制
您正在循环中调用它,在第一次迭代之后,将按照您所描述的那样创建它
而不是代码:
for (ListFileItem item : list.getResults()) {
URI uri = item.getUri();
String path = uri.getPath();
String localPath = localRootDirPath;
String itemName = new File(path).getName();
CloudFile file = root.getFileReference(itemName);
file.download(new FileOutputStream(localRootDirPath+itemName));
CloudFile backupFile = backup.getFileReference(itemName);
backupFile.startCopy(file);
file.delete();
file.download(new
FileOutputStream(localRootDirPath+"ok_business"));
}
使用这种方法。将CloudFile文件
文件声明移到for循环之外
。并将最后一个表达式从循环的中移出
CloudFile file;
for (ListFileItem item : list.getResults()) {
URI uri = item.getUri();
String path = uri.getPath();
String localPath = localRootDirPath;
String itemName = new File(path).getName();
file = root.getFileReference(itemName);
file.download(new FileOutputStream(localRootDirPath+itemName));
CloudFile backupFile = backup.getFileReference(itemName);
backupFile.startCopy(file);
file.delete();
}
// preventing Null Pointer Exception for cases, when file is not initialized
if (file!=null) {
file.download(new FileOutputStream(localRootDirPath+"ok_business"));
}
那么你就不应该在循环中这样做。这样做,他就不会引用文件
对象。@Sahilkhan很高兴,我帮了你:)嗨@Rafael,我又遇到了一个问题。“使用java提取XML的所有重复元素”如果可能,请您帮助我。@Sahilkhan对于单独的问题,请在StackOverflow上发表单独的问题帖子。并查看这些链接:@Rafael,是的,问题已经在10天前发布了。:)