【发布时间】:2011-07-01 14:34:49
【问题描述】:
我有一个代码来连接文件夹中的 txt 文件并将连接的文件移动到另一个文件夹中。 我的代码运行良好,但在连接它们后会删除文件,所以我想在连接它们后将这些文件移动到另一个文件夹。
我的 c:\source 中的文件必须移动到 c:\Archive
一开始是我的错误,我想移动文件但我删除了它们!! 当源文件夹中没有文件时,我想抛出异常。
所以我的代码是:
PrintWriter pw = new PrintWriter(new FileOutputStream("C:/Target/Filec.txt"));// directory where concatenated file are created
File file = new File("C:/Source");// where files have to be concatenated and move to c:\Archive before deleting
File[] files2 = file.listFiles();
for (int i = 0; i < files2.length; i++)
{
File currentFile = files2[i];
System.out.println("Processing " + currentFile.getPath() + "... ");
BufferedReader br = new BufferedReader(new FileReader(currentFile));
String line = br.readLine();
while (line != null)
{
pw.println(line);
line = br.readLine();
}
br.close();
if (!currentFile.delete())
{
// Failed to delete file
System.out.println("Failed to delete "+ currentFile.getName());
}
}
pw.close();
System.out.println("All files have been concatenated into Filec.txt");
}
}
谢谢
【问题讨论】: