我的代碼:的Java從.txt文件中讀取數據,並將其輸出到output.txt的文件
import java.io.*;
public class ProgramSix {
public int count(String fileName, char charToSerach) throws FileNotFoundException, IOException {
char lower = Character.toLowerCase(charToSerach);
char upper = Character.toUpperCase(charToSerach);
int count = 0;
try (BufferedReader reader = new BufferedReader(new FileReader("xanadu.txt"))) {
int ch = 0;
while ((ch = reader.read()) != -1) {
if (lower == ch || upper == ch) {
count++;
}
}
}
System.out.println("The character '" + charToSerach + "' appears: " + count + " times in the .txt file");
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Program6 program6 = new Program6();
String fileName = "xanadu";
int numAs = programSix.count(fileName, 'a');
int numBs = programSix.count(fileName, 'b');
int numCs = programSix.count(fileName, 'c');
} catch (Throwable e) {
e.printStackTrace();
}
}
}
所以現在我的代碼正確地給出了輸出:給予時代的人物的一個'數b'和'c'出現在我的.txt文件中。現在讀取相同的.txt文件並在output.txt文件中發出三個不同的字母以及它們發生的次數的最佳方式是什麼?
你得到什麼作爲輸出? –
該檔案是否存在? –
也許一個異常正在發生......但是你可以通過給你的代碼添加一個斷點和_debugging_來自己回答這個問題。 –