所以我有以下代碼:的Java BufferedReader中的FileReader問題
import java.io.*;
public class Plagiarism {
public static void main(String[] args) {
Plagiarism myPlag = new Plagiarism();
if (args.length == 0) {
System.out.println("Error: No files input");
}
else if (args.length > 0) {
try {
for (int i = 0; i < args.length; i++) {
BufferedReader reader = new BufferedReader (new FileReader (args[i]));
simplify (reader);
reader.close();
}
}
catch (Exception e) {
System.err.println ("Error reading from file");
}
}
}
public static void simplify(BufferedReader input) throws IOException {
String line = null;
line = input.readLine();
while (line != null) {
line = line.replaceAll ("[^a-zA-Z0-9 ]", "");
line = line.toLowerCase();
}
}
}
這段代碼的問題是,它編譯,但是當我運行它,並添加在命令行如2個參數。 Java抄襲text1.txt text2.txt。編輯:當我運行它,它只是不做任何事情,甚至沒有完成,就像它被卡住的地方。
感謝您的任何幫助。
在'catch'子句中添加'System.out.println(e.getMessage());'來了解異常情況。 – ltalhouarne
首先不抓住'Exception',而是更具體的一個。 'Exception'還捕獲'RuntimeException'並派生,因此所有未經檢查的異常。 – fge
打印您的Stacktrace(e.printStackTrace())。 –