Java从.txt文件读取数据并将其输出到output.txt文件

时间:2017-06-19 06:12:59

标签: java

我的代码:

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();
        }
    }
}

所以现在我的代码正确地给出了输出:给出了字符'a''b'和'c'出现在我的.txt文件中的次数。现在,读取相同的.txt文件并在output.txt文件中输出三个不同的字母以及它们出现的次数是什么?

1 个答案:

答案 0 :(得分:-1)

您使用的是错误的班级

class  ProgramSix 

VS

Program6 program6 = new Program6();