英文:
Saving sucessfully copied Files from A to B in Java when Programm crashes?
问题
package com.mycompany;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
public class CopyingData {
public static void saveFilesAlreadyCopied(ArrayList<String> filesToSave, String fileName) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String file : filesToSave) {
pw.println(file);
}
pw.close();
}
public static void main(String[] args) {
// Variables:
File sourceDir = new File("sourceDir");
File destinationDir = new File("destDir");
ArrayList<String> listOfFiles = new ArrayList<>();
ArrayList<String> filesAlreadyCopied = new ArrayList<>();
// Create dirs:
sourceDir.mkdirs();
destinationDir.mkdirs();
// Generate 1000 Files for Testing:
for(int i = 0; i < 1000; i++) {
File textFile = new File("sourceDir/textfile" + i + ".txt");
try {
textFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
listOfFiles.add(textFile.getAbsolutePath());
}
// Copy files from sourceDir to destinationDir:
for(String file : listOfFiles) {
Path sourcePath = Paths.get(file);
Path destPath = Paths.get(destinationDir.getAbsolutePath() + File.separator + new File(file).getName());
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
filesAlreadyCopied.add(file);
saveFilesAlreadyCopied(filesAlreadyCopied, sourceDir.getAbsolutePath() + File.separator + "backupCopiedFiles.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
英文:
i have to copy lots of files from A to B in Java, problem is that the Programm might crash.
Already sucessfully copied Files cannot be copied again, when the programm restarts. How can I save in a textFile already copied Files?
My solution below is not 100% sucessfull, sometimes it doesnt save all the successfully copied files.
I wonder if theres a better approach to this issue.Thanks a lot in advance.
package com.mycompany;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
public class CopyingData {
public static void saveFilesAlreadyCopied(ArrayList<String> filesToSave, String fileName) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String file : filesToSave) {
pw.println(file);
}
pw.close();
}
public static void main(String[] args) {
//Variables:
File sourceDir = new File("sourceDir");
File destinationDir = new File("destDir");
ArrayList<String> listOfFiles = new ArrayList<>();
ArrayList<String> filesAlreadyCopied = new ArrayList<>();
//Create dirs:
sourceDir.mkdirs();
destinationDir.mkdirs();
//Generate 1000 Files for Testing:
for(int i = 0;i<1000;i++) {
File textFile = new File("sourceDir/textfile"+i+".txt");
try {
textFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listOfFiles.add(textFile.getAbsolutePath());
}
//Copy files from sourceDir to destinationDir:
for(String file : listOfFiles) {
Path sourcePath = Paths.get(file);
Path destPath = Paths.get(destinationDir.getAbsolutePath() + File.separator + new File(file).getName());
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
filesAlreadyCopied.add(file);
saveFilesAlreadyCopied(filesAlreadyCopied,sourceDir.getAbsolutePath() + File.separator +"backupCopiedFiles.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案1
得分: 1
一个可能的问题是您可能会耗尽磁盘空间,我能理解在复制了数百万个文件、数太字节的数据之后,在最后一个文件崩溃时,您希望释放一些磁盘空间并继续处理最后一个文件。
但请考虑以下情况:所编写的算法基本如下(假设检查点文件 "chkpt" 包含成功复制的文件列表):
- 对于每个文件
f
, - 复制文件
f
, - 更新检查点文件
chkpt
。
因此,如果步骤 #2 未能写入文件系统,那么您可以根据 chkpt
的内容恢复。但如果步骤 #3 未能写入文件系统会发生什么呢?(您会为检查点文件创建另一个检查点文件吗?)这种情况发生的几率会随着 chkpt
的大小增加而增加,而您当前将其设置为包含要复制的所有文件的完整列表(对于大量文件的情况可能会成为问题)。
我可能会推荐以下方法(供您思考),以减少无法恢复的可能性:
- 如果您想要保留要复制的文件列表,那么首先在磁盘上创建一个文件,只创建一次;例如,
filelist.txt
,其中包含file1.txt
,file2.txt
等。 - 在开始复制之前,创建一个名为
copying
的文件,其中包含“下一个要复制的文件”,例如,如果您已经复制了文件file1.txt
到file31.txt
,那么它将包含file32.txt
。 - 开始复制文件(例如
file32.txt
)。 - 如果复制失败并且程序崩溃,文件
copying
的存在表明了停止的位置 —— 重新启动复制file32.txt
。 - 如果复制成功,请将
copying
做一个“移动”操作(文件重命名),改为completed
(因此,文件completed
将包含文件名file32.txt
……当然,文件名可以随意设置)。文件的移动/重命名通常不会失败,也不应像写入大文件那样经常出现损坏。 - 继续处理下一个文件:使用列表中的下一个文件名创建一个新文件
copying
(不要修改文件列表)。
这样做有意义吗?(您有何想法?)与您目前拥有的内容相比,以上工作量不会增加太多,但可能会降低遇到“无法恢复”的情况的几率(即损坏的检查点文件)。
英文:
One possible problem you might have is running out of disk space, and I can see how after copying millions of files, terabytes of data, & then crashing on the last file, you would want to just free up some disk space & continue with the last file.
But consider this: the algorithm as written is basically as follows (assuming a checkpoint file "chkpt" contains list of files successfully copied):
- for each file
f
, - copy file
f
, - update checkpoint file
chkpt
So, if step #2 fails to write to the file system, then you can resume based on the contents of chkpt
. But what happens if step #3 fails to write to the file system? (Do you create a checkpoint file for the checkpoint file?) The odds of this happening increase with the size of chkpt
, which you currently have as containing the entire list of files to copy (could be a problem for a large list of files).
I might recommend the following (as food for thought) to decrease the odds of being unable to recover:
- if you want to persist a list of files to copy, then create that first on disk, just once; e.g.,
filelist.txt
, containingfile1.txt
,file2.txt
, etc - before starting the copy, create a file called e.g.
copying
containing the "next file to copy", e.g., if you've already copied filesfile1.txt
throughfile31.txt
, it would containfile32.txt
- start to copy the file (eg
file32.txt
) - if the copy fails, and the program crashes, the existence of a file called
copying
indicates where things stopped -- restart copy offile32.txt
- if the copy succeeded, do a "move" (file rename) of
copying
tocompleted
(so, filecompleted
would contain filenamefile32.txt
...also, obviously, call these files whatever you want). A file move/rename won't usually fail, nor should it become corrupted as often as writing a large file. - continue with the next file: create a new file
copying
with the next filename in the list (don't modify the file list)
Does that make sense? (Thoughts?) The level of effort above what you currently have isn't much more, but it might reduce the chance of running into an "unrecoverable" state (i.e., corrupted checkpoint file)..
答案2
得分: 0
我已对您的代码进行了一些更改,以处理失败情况,请尝试以下代码,让您的老板满意:
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class CopyingData {
public static void saveFilesAlreadyCopied(ArrayList<String> filesToSave, String fileName) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String file : filesToSave) {
pw.println(file);
}
pw.close();
}
public static void main(String[] args) throws IOException {
String operationFolder = "C:/Downloads/code_play-folder/";
// 变量:
File sourceDir = new File(operationFolder + "sourceDir");
File destinationDir = new File(operationFolder + "destDir");
String progressFilePath = operationFolder + "sourceDir" + File.separator + "backupCopiedFiles.txt";
File progressReportFile = new File(progressFilePath);
ArrayList<String> listOfFiles = new ArrayList<>();
ArrayList<String> filesAlreadyCopied = new ArrayList<>();
if (progressReportFile.exists()) {
filesAlreadyCopied.addAll(Files.readAllLines(Paths.get(progressFilePath)).stream().filter(data -> !data.trim().equals("")).collect(Collectors.toList()));
}
// 创建目录:
sourceDir.mkdirs();
destinationDir.mkdirs();
// 生成1000个用于测试的文件:
for (int i = 0; i < 1000; i++) {
String fileName = operationFolder + "sourceDir/textfile" + i + ".txt";
File textFile = new File(fileName);
try {
if (!textFile.exists()) {
textFile.createNewFile();
Files.write(
Paths.get(fileName),
("contentToAppend" + fileName).getBytes(),
StandardOpenOption.APPEND);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listOfFiles.add(textFile.getAbsolutePath());
}
listOfFiles.removeAll(filesAlreadyCopied);
// 从sourceDir复制文件到destinationDir:
for (String file : listOfFiles) {
Path sourcePath = Paths.get(file);
Path destPath = Paths.get(destinationDir.getAbsolutePath() + File.separator + new File(file).getName());
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
filesAlreadyCopied.add(file);
saveFilesAlreadyCopied(filesAlreadyCopied, sourceDir.getAbsolutePath() + File.separator + "backupCopiedFiles.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
英文:
I have made some changes to your code, to handle fail scenarios, please try below code and make happy your boss:
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class CopyingData {
public static void saveFilesAlreadyCopied(ArrayList<String> filesToSave, String fileName) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String file : filesToSave) {
pw.println(file);
}
pw.close();
}
public static void main(String[] args) throws IOException {
String operationFolder = "C:/Downloads/code_play-folder/";
//Variables:
File sourceDir = new File(operationFolder+"sourceDir");
File destinationDir = new File(operationFolder+"destDir");
String progressFilePath = operationFolder+"sourceDir" +File.separator + "backupCopiedFiles.txt";
File progressReportFile = new File(progressFilePath);
ArrayList<String> listOfFiles = new ArrayList<>();
ArrayList<String> filesAlreadyCopied = new ArrayList<>();
if (progressReportFile.exists()){
filesAlreadyCopied.addAll(Files.readAllLines(Paths.get(progressFilePath)).stream().filter(data-> !data.trim().equals("")).collect(Collectors.toList()));
}
//Create dirs:
sourceDir.mkdirs();
destinationDir.mkdirs();
//Generate 1000 Files for Testing:
for(int i = 0;i<1000;i++) {
String fileName = operationFolder+"sourceDir/textfile" + i + ".txt";
File textFile = new File(fileName);
try {
if (!textFile.exists()) {
textFile.createNewFile();
Files.write(
Paths.get(fileName),
("contentToAppend"+fileName).getBytes(),
StandardOpenOption.APPEND);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listOfFiles.add(textFile.getAbsolutePath());
}
listOfFiles.removeAll(filesAlreadyCopied);
//Copy files from sourceDir to destinationDir:
for(String file : listOfFiles) {
Path sourcePath = Paths.get(file);
Path destPath = Paths.get(destinationDir.getAbsolutePath() + File.separator + new File(file).getName());
try {
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
filesAlreadyCopied.add(file);
saveFilesAlreadyCopied(filesAlreadyCopied,sourceDir.getAbsolutePath() + File.separator +"backupCopiedFiles.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论