文本文件的字符数

时间:2013-11-20 11:02:35

标签: java while-loop counter bufferedreader println

我希望在java中对全小写字符.txt文件执行字符计数,目前打印方法在尝试打印计数时最终为空白。任何帮助将不胜感激。

public static void main(String[] args) throws IOException {
    File file1 = new File("newfile.txt");
    BufferedReader in = new BufferedReader (new FileReader (file1));
    System.out.println("Relative Letter Frequency");

    int ch;
    char i = 0;
    // Declare 26 char counting
    int[] count = new int[26];
    //Loop through the file char
    while ((ch = in.read()) != -1) {
    i = (char) ch;
        if (i >= 'a' && i <= 'z')
        count[i - 'a']++;
    }

    //Print loop
    for (int j = 0; j < 26; j++) {
        //Print formatting so each letter is represented on
        //a new line with correct spacing
        System.out.printf("%c %d \n", j + 'a', count[j]);
        System.out.println();
    }

    in.close();
}

int Counter(File file,String Charset) throws IOException {
        File file1 = new File("newfile.txt");
        BufferedReader in = new BufferedReader( new FileReader(file1));

        int Count = 0;
        while (in.read() > -1){
            Count++;

        }
        System.out.println(Count);
        in.close();     
        return Count;
    }
}

我试图改变字符计数器的循环,但仍无济于事

            public void Counter(File file,String Charset) throws IOException {
        File file1 = new File("newfile.txt");
        BufferedReader in = new BufferedReader(new FileReader(file1));
        int CH;
        char k = 0;
        int [] Count = new int [1];
        while ((CH = in.read()) != -1){
            k = (char) CH;
            if (k >= 'a' && k <='z')
            Count [k]++;    
        }
    System.out.println(Count[k]);
        in.close();
    }

2 个答案:

答案 0 :(得分:2)

由于我慢慢意识到你需要什么,我已经调整了你的代码,以显示两种方法。

以下是两次读取文件的示例。第一次使用BufferedReader逐行读取字符数。

下面的第二个解决方案是逐个字节地读取填充内容并按照您可能尝试的方式对字符进行计数。

package snippet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class Snippet {
  public static void main(String[] args) throws IOException {
    System.out.println("Sum: " + counter(new File("newfile.txt")));
  }

  public static int counter(File file) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line;
    int[] counter = new int[26];
    while ((line = in.readLine()) != null) {
      for (char c : line.toCharArray()) {
        if (c >= 'a' && c <= 'z') {
          counter[c - 'a']++;
        }
      }
    }

    InputStream is = new FileInputStream(file);
    int read;
    while ((read = is.read()) != -1) {
      if ((read >= 'a') && (read <= 'z')) {
        counter[read - 'a']++;
      }
    }

    int result = 0;
    for (int count : counter) {
      System.out.println(count);
      result += count;
    }
    return result;
  }
}

答案 1 :(得分:0)

我知道这可能听起来非常愚蠢,但为什么不只是使用file.ToString的长度? 你想跳过空格和回车等吗? 只是在寻找一些关于你在这里尝试做什么的澄清......

或者简单地遍历每个SubString并创建一个函数来返回它是否&#34; A&#34;或&#34; a&#34;等等.. 我不是C编码员,但我习惯用C ++做一些工作,所以,我会尝试用我理解的语言为你概述......

Public Function GetIsCharacter(ByVal sCharacter As String) As Boolean

 If sCharacter = "A" Then ' etc...
   Return True
 End If

' else...
Return False

End Function