如何用 java 将 5 个以上的 txt 文件合并到 1 个文件中?
How can merge more than 5 txt file in 1 file with java?
我想将多个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();
}
}
}
我将上面的代码重写如下,运行正常
public static void mergeFile() throws IOException {
long unixTime = System.currentTimeMillis() / 1000L;
final String sysName = String.valueOf(unixTime);
File directory = new File("src/raw/");
File[] files = directory.listFiles();
FileWriter myWriter = new FileWriter("src/ok/"+sysName+".txt");
for(File file : files) {
String aLine;
String fileName = file.toString();
int split = fileName.indexOf(".");
long fName = Long.parseLong(fileName.substring(15, split)) ;
if (fName <= unixTime) {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
while ((aLine = in.readLine()) != null) {
myWriter.write(aLine);
myWriter.write("\n");
}
in.close();
}
Files.move(Paths.get("src/raw/"+fName+".txt"),
Paths.get("src/old/"+fName+".txt"));
}
myWriter.close();
Query.insertSyslog("src/ok/"+sysName+".txt");
}
我想将多个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();
}
}
}
我将上面的代码重写如下,运行正常
public static void mergeFile() throws IOException {
long unixTime = System.currentTimeMillis() / 1000L;
final String sysName = String.valueOf(unixTime);
File directory = new File("src/raw/");
File[] files = directory.listFiles();
FileWriter myWriter = new FileWriter("src/ok/"+sysName+".txt");
for(File file : files) {
String aLine;
String fileName = file.toString();
int split = fileName.indexOf(".");
long fName = Long.parseLong(fileName.substring(15, split)) ;
if (fName <= unixTime) {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
while ((aLine = in.readLine()) != null) {
myWriter.write(aLine);
myWriter.write("\n");
}
in.close();
}
Files.move(Paths.get("src/raw/"+fName+".txt"),
Paths.get("src/old/"+fName+".txt"));
}
myWriter.close();
Query.insertSyslog("src/ok/"+sysName+".txt");
}