Java帮助从文件中读取

时间:2010-12-01 22:21:38

标签: java file-io

我有一段代码,我不知道该怎么做。它应该显示一个选择文件的对话框,当选择文件时,它会在组织仪中输出结果。我有一切工作,除了我无法弄清楚我需要把什么变量作为text2。

private int[] countLetters2()
 {
  // Count for 26 letters
  int[] count = new int[26];

  //get contents from file  
  chooser.showOpenDialog(null);
  File f = chooser.getSelectedFile();

  try
  {
   FileReader fr = new FileReader(f);
   BufferedReader br = new BufferedReader(fr);
   String s;
   while((s = br.readLine()) != null)
   {
    System.out.println(s);   <------simply to see if the reader and buffer were working

   }
  }
  catch(IOException g) {} 


  String text2 = ; <------------------------------------this is the problem

  //converts every letter to uppercase
  text2 = text2.toUpperCase();  

  //Count occurrence of each letter (case insensitive)
  for (int i = 0; i < text2.length(); i++)
  {
   char character = text2.charAt(i);

   if ((character >= 'A') && (character <= 'Z'))
   {
    count[(int)character - 65]++; // The ASCII for 'A' is 65
   }
  }
  return count; // Return the count array 

3 个答案:

答案 0 :(得分:0)

只需在while循环中收集你的text2变量。

         String text2 = "";
            try { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s="" while((s = br.readLine()) != null) {
text2+=s; 
ystem.out.println(s);

            } } catch(IOException g) {}

OR

String s = "";
            try { FileReader fr = new FileReader(f); 
            BufferedReader br = new BufferedReader(fr);
            String tmp;
            while((tmp = br.readLine()) != null) {              
                System.out.println(s);
                s += tmp;
            } } catch(IOException g) {}

            text2=tmp;

答案 1 :(得分:0)

将计算字符数的循环移动到打印行的循环中。像

这样的东西
private int[] countLetters2()
 {
  // Count for 26 letters
  int[] count = new int[26];

  //get contents from file  
  chooser.showOpenDialog(null);
  File f = chooser.getSelectedFile();

  try
  {
   FileReader fr = new FileReader(f);
   BufferedReader br = new BufferedReader(fr);
   String s;
   while((s = br.readLine()) != null)
   {
    System.out.println(s);   
    //converts every letter to uppercase
    String text2 = s.toUpperCase();  

    //Count occurrence of each letter (case insensitive)
    for (int i = 0; i < text2.length(); i++)
    {
       char character = text2.charAt(i);

       if ((character >= 'A') && (character <= 'Z'))
       {
        count[(int)character - 65]++; // The ASCII for 'A' is 65
       }
    }
   }
  }
  catch(IOException g) {} 

  return count;
 }

答案 2 :(得分:0)

我认为使用BufferedReader逐行浏览文件然后逐个字符地遍历每一行是没有意义的。这是不必要的复杂和低效,因为你基本上只想迭代所有字符:这正是Reader首先给你的功能。

您可以大大简化您的日常工作:

    [...]

    BufferedReader br = new BufferedReader(fr);
    for (int c; (c = br.read()) > -1;) {
        char character = Character.toUpperCase((char) c);

        if ((character >= 'A') && (character <= 'Z')) {
            count[character - 'A']++;
        }
    }
} catch (IOException g) { /* this is no good */ }

return count;

这样你就可以完全摆脱text2,你的代码更短,你的程序运行得更快(如果可以测量的话),因为不必要的String构造会被删除。

P.S。:一个不相关的注释:确保处理用户在文件选择器对话框中单击“取消”的情况。

P.P.S。:用户是否可以在程序中无意中选择目录而不是正确的文件?那会发生什么?