Java 如何使用exception使代码在列出的所有内容中运行?
我有两个具有相同文件夹树结构和文件名的目录。这些是本周和上周的数据。我将把新数据添加到旧数据中,每对数据都要命名,并将其保存在另一个具有原始文件树结构的目录下 如果我想让exception/use try catch通过skip(如果目录中不存在列出的所有文件),如何抛出exception/use try catch 还是我犯了什么错误Java 如何使用exception使代码在列出的所有内容中运行?,java,exception,try-catch,Java,Exception,Try Catch,我有两个具有相同文件夹树结构和文件名的目录。这些是本周和上周的数据。我将把新数据添加到旧数据中,每对数据都要命名,并将其保存在另一个具有原始文件树结构的目录下 如果我想让exception/use try catch通过skip(如果目录中不存在列出的所有文件),如何抛出exception/use try catch 还是我犯了什么错误 import java.io.*; import java.util.ArrayList; import java.util.List; public cla
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Merge {
public static void main(String[] args) {
List<String> fileListA = new ArrayList<String>();
if (args.length != 3) {
System.out.println("Usage: Merge -DirA -DirB -OutputDir");
} else {
System.out.println("Path:" + args[0]);
// Read all text file from the specified path
fileListA = getInputFileNames(args[0], "txt");
int count = 0;
for (String str : fileListA){
String data;
// Path of files form DirA and DirB
String filePathA = str;
System.out.println("PathA:" + filePathA);
String filePathB = str.replace(args[0], args[1]);
System.out.println("PathB:" + filePathB);
// Buffer for reading fileA and fileB
BufferedReader brA = new BufferedReader(new FileReader(filePathA));
BufferedReader brB = new BufferedReader(new FileReader(filePathB));
// Create fileOut object
File fileOut = new File(str.replace(args[0], args[2]));
System.out.println("PathOut:" + str.replace(args[0], args[2]));
// Create fileOut if doesn't exist
if (!fileOut.exists()) {
fileOut.createNewFile();
}
// Buffer for writing fileOut
BufferedWriter bw = new BufferedWriter(new FileWriter(fileOut.getAbsoluteFile()));
// Read through fileA and fileB, append each line to fileOut
while ((data = brA.readLine()) != null) {
bw.write(data);
bw.newLine();
}
brA.close();
while ((data = brB.readLine()) != null) {
bw.write(data);
bw.newLine();
}
brB.close();
bw.close();
count++;
}
System.out.println("Files done"+ count);
}
}
public static List<String> getInputFileNames(String sDir, String ext){
// Get all file names from a given path, recursively go into each sub-directory as well
//System.getProperty("file.separator");
List<String> results = new ArrayList<String>();
File [] files = new File(sDir).listFiles();
for (File file : files){
if (file.isFile()){
String fName = file.getAbsolutePath();
if (fName.substring(fName.lastIndexOf(".")+1).equalsIgnoreCase(ext)){
results.add(fName);
}
} else if (file.isDirectory()){
List<String> subResults = getInputFileNames(file.getPath(), ext);
if (!subResults.isEmpty()){
results.addAll(subResults);
}
} else {
// Not a file or directory
}
}
return results;
}
}