在java中使用fileReader一次读取一个字符

时间:2014-06-02 04:46:51

标签: java arrays file-io filereader readline

作为学校练习的一部分,我试图从文本文件中读取字符并计算出现在文本文件中的字符的频率。我将频率存储在一个数组中,其中索引是char的ASCII代码,数组中的数字是频率。

   int c;
  FileReader fr = new FileReader (inputFile);
  int [] freq = new int [200];
     while ( (c= fr.read())!= -1){
        int index = c;
        freq [index]= freq [index]+1;
  }
  PrintWriter pw = new PrintWriter(new FileWriter(outputFile));

  for (int i =0; i<frequency.length; i++) {
      if(frequency[i]!=0){
          pw.println( ((char)i) + " " +frequency[i]);

不知何故,此方法仅适用于带有单行的文本文件,例如&#34; abcdefgh&#34;。它不适用于包含多行的文件,例如&#34; ab / newline cde /newline ...。&#34;对于这种类型的文件,当我打印出数组时,它会在结果的顶部生成一个空行和一些数字。我真的无法弄清楚原因。

1 个答案:

答案 0 :(得分:0)

对我来说很好看。

import java.io.FileReader;
import java.util.Arrays;

public class Foo {
    public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("/tmp/a");
        int[] freq = new int[200];
        int c;
        while ((c = fr.read()) != -1) {
            freq[c] = freq[c] + 1;
        }
        System.out.println(Arrays.toString(freq));
    }
}

/tmp/a的示例内容:

abc
def

输出:

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0]

编辑 - 回复修订后的问题:

输出

2 a 1 b 1 c 1 d 1 e 1 f 1

该文件有两个换行符,所以程序正在写一个换行符,然后是&#34; 2&#34;。

我猜你想要将字符转换为类似Java转义序列的东西。这是使用Apache commons-lang的解决方案:

import org.apache.commons.lang3.StringEscapeUtils;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Foo {

    public static void main(String[] args) throws Exception {
        write(read());
    }

    static int[] read() throws IOException {
        FileReader fr = new FileReader("/tmp/a");
        int[] freq = new int[200];
        int c;
        while ((c = fr.read()) != -1) {
            freq[c] = freq[c] + 1;
        }
        return freq;
    }

    static void write(int[] freq) throws IOException {
        try (PrintWriter pw = new PrintWriter(new FileWriter("/tmp/b"))) {
            for (int i = 0; i < freq.length; i++) {
                if (freq[i] != 0) {
                    char c = (char) i;
                    String s = StringEscapeUtils.escapeJava("" + c);
                    pw.println(s + " " + freq[i]);
                }
            }
        }
    }
}

输出:

\n 2
a 1
b 1
c 1
d 1
e 1
f 1