我想在一个txt文件中保存多个txt文件并删除以前的文件。我可以用什么方法?我自己写的代码:
public static void mergeFile() throws IOException {
long unixTime = System.currentTimeMillis() / 1000L;
final String sysName = String.valueOf(unixTime);
File directory = new File("src/");
FileWriter myWriter = new FileWriter("src/"+sysName+".txt",true);
File[] files = directory.listFiles();
for(File file : files) {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
String fileName = file.toString();
int split = fileName.indexOf(".");
long fName = Long.parseLong(fileName.substring(5, split)) ;
if (fName < unixTime) {
while ((aLine = in.readLine()) != null) {
myWriter.write(aLine);
myWriter.write("\n");
}
in.close();
file.delete();
}
}
}
1条答案
按热度按时间gg58donl1#
我重写了上面的代码如下,它工作正常。