0
我需要一個名爲wonder1.txt
的文件並將該文件中的日期寫入另一個文件。可以說我有更多的文件,如wonder2.txt
,wonder3.txt
,wonder4.txt
。我如何在同一個文件中編寫其餘部分。從多個文件中取數據並移動到一個文件
import java.io.*;
import java.util.*;
import java.lang.*;
public class alice {
public static void main(String[] args) throws FileNotFoundException, IOException {
String fileName = ("/Users/DAndre/Desktop/Alice/wonder1.txt");
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
StringBuilder stringBuilder = new StringBuilder();
String line = br.readLine();
while (line != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
line = br.readLine();
}
/**
* Pass original file content as string to another method which
* creates new file with same content.
*/
newFile(stringBuilder.toString());
} finally {
br.close();
}
}
public static void newFile(String fileContent) {
try {
String newFileLocation = "/Users/DAndre/Desktop/Alice/new1.txt";
PrintWriter writer = new PrintWriter(newFileLocation);
writer.write(fileContent);//Writes original file content into new file
writer.close();
System.out.println("File Created");
} catch (Exception e) {
e.printStackTrace();
}
}
}
的所有文件都在一個文件夾中 – unlimited4311 2014-11-09 08:07:37
我不本部規定:對(最終文件FileEntry的:folder.listFiles()){ 字符串文件名= fileEntry.getName() ; – unlimited4311 2014-11-09 08:18:47
我的不好,再次編輯它。所以變量'文件夾'代表你保存文件的文件夾。確保你寫的文件位於不同的文件夾中,否則它會讀取它並重寫它的內容。 – 2014-11-09 08:25:57