我的代码:的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_来自己回答这个问题。 –