The scenario is as follows:
path1 and path2 are directories of two folders. There are txt files with the same name in the directories, but the contents are different. It is necessary to merge the contents of the files with the same name in different directories. For example: there is a file a.txt
in the directory path1 , the content is as follows: 1 2 b.txt, the content is as follows: 6 7 There is a file a.txt in the directory path2 , the content is as follows: 3 4 b.txt, the content is as follows: 8 9 After merging, the content of a.txt in the directory path1 is: 1 2 3 4 b.txt, the content is as follows: 6 7 8 9 [java] view plain copy import java.io.*; import java.util.ArrayList; import java.util.List; public class HandleFile {
public static void mergeFileContent(String path1, String path2) throws IOException {
File f1 = new File(path1);
File f2 = new File(path2);
//Return the name list of all files in this folder
String[] name1 = f1 .list();
for(int m = 0; m < name1.length; m++) {
System.out.println(name1[m]);
}
String[] name2 = f2.list();
//list is temporarily stored The content of a single file under path2
List list = new ArrayList();
for(int i=0; i<name1.length; i++){
for(int j=0; j < name2.length; j++) {
//Judgment 2 In each folder, whether the file has the same name; yes, write the content of file 2 into file 1; and judge that it is a txt file
if( name1[i].equals(name2[j]) && name1[i].endsWith(".txt") ) {
try {
String path4 = path2 + name2[j];
File file2 = new File(path4);
BufferedReader bw;
bw = new BufferedReader(new FileReader(file2));
String line = null;
//先存入list集合中
while((line = bw.readLine()) != null) {
list.add(line);
}
//合并到文件1
String path3 = path1 + name1[i];
File file1=new File(path3);
BufferedWriter out=new BufferedWriter(new FileWriter(file1,true));
for (int k=0; k<list.size(); k++) {
out.write((String) list.get(k));
out.newLine();
}
list.clear();
bw.close();
bw=null;
file2=null;
out.close();
out=null;
file1=null;
//If a file A in directory 1 finds a file A with the same name in directory 2, it will not continue to judge in directory 2
break;
} catch (FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//Add the file name.txt
public static void suffixAdd(String path) {
//Check whether the file to be renamed exists and whether is a file
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
File[] files = file.listFiles();//Get the list of files in this directory
for (File fileFrom : files) {
String fromFile = fileFrom.getName();//File name
String toFileName;
toFileName = fromFile + ".txt";
//Get the root path of the file
String rootPath = fileFrom.getParent();
//Need to add the original Root path
File toFile = new File(rootPath + File.separator + toFileName);
System.out.println("Modified file name: " + toFile.getName());
if (fileFrom.exists() && !toFile. exists()) {
//开始更名
fileFrom.renameTo(toFile);
}
}
} else {
System.out.println("File does not exist: " + file);
return;
}
}
public static void main(String[] a) throws IOException{
//
String path1 = "e:\\merge\\test1\\";
String path2 = "e:\\merge\\test2\\";
mergeFileContent(path1, path2);
String path = "E:\\addtest\\";
suffixAdd(path);
}
}
path1 and path2 are directories of two folders. There are txt files with the same name in the directories, but the contents are different. It is necessary to merge the contents of the files with the same name in different directories. For example: there is a file a.txt
in the directory path1 , the content is as follows: 1 2 b.txt, the content is as follows: 6 7 There is a file a.txt in the directory path2 , the content is as follows: 3 4 b.txt, the content is as follows: 8 9 After merging, the content of a.txt in the directory path1 is: 1 2 3 4 b.txt, the content is as follows: 6 7 8 9 [java] view plain copy import java.io.*; import java.util.ArrayList; import java.util.List; public class HandleFile {
public static void mergeFileContent(String path1, String path2) throws IOException {
File f1 = new File(path1);
File f2 = new File(path2);
//Return the name list of all files in this folder
String[] name1 = f1 .list();
for(int m = 0; m < name1.length; m++) {
System.out.println(name1[m]);
}
String[] name2 = f2.list();
//list is temporarily stored The content of a single file under path2
List list = new ArrayList();
for(int i=0; i<name1.length; i++){
for(int j=0; j < name2.length; j++) {
//Judgment 2 In each folder, whether the file has the same name; yes, write the content of file 2 into file 1; and judge that it is a txt file
if( name1[i].equals(name2[j]) && name1[i].endsWith(".txt") ) {
try {
String path4 = path2 + name2[j];
File file2 = new File(path4);
BufferedReader bw;
bw = new BufferedReader(new FileReader(file2));
String line = null;
//先存入list集合中
while((line = bw.readLine()) != null) {
list.add(line);
}
//合并到文件1
String path3 = path1 + name1[i];
File file1=new File(path3);
BufferedWriter out=new BufferedWriter(new FileWriter(file1,true));
for (int k=0; k<list.size(); k++) {
out.write((String) list.get(k));
out.newLine();
}
list.clear();
bw.close();
bw=null;
file2=null;
out.close();
out=null;
file1=null;
//If a file A in directory 1 finds a file A with the same name in directory 2, it will not continue to judge in directory 2
break;
} catch (FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//Add the file name.txt
public static void suffixAdd(String path) {
//Check whether the file to be renamed exists and whether is a file
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
File[] files = file.listFiles();//Get the list of files in this directory
for (File fileFrom : files) {
String fromFile = fileFrom.getName();//File name
String toFileName;
toFileName = fromFile + ".txt";
//Get the root path of the file
String rootPath = fileFrom.getParent();
//Need to add the original Root path
File toFile = new File(rootPath + File.separator + toFileName);
System.out.println("Modified file name: " + toFile.getName());
if (fileFrom.exists() && !toFile. exists()) {
//开始更名
fileFrom.renameTo(toFile);
}
}
} else {
System.out.println("File does not exist: " + file);
return;
}
}
public static void main(String[] a) throws IOException{
//
String path1 = "e:\\merge\\test1\\";
String path2 = "e:\\merge\\test2\\";
mergeFileContent(path1, path2);
String path = "E:\\addtest\\";
suffixAdd(path);
}
}