【问题标题】:Code that can read in letters in a .txt file可以读取 .txt 文件中字母的代码
【发布时间】:2015-12-14 19:42:20
【问题描述】:

我正在编写一个程序,它应该读取一个简单的文本文件并输出该 .txt 文件中所有字母的列表,按最常用字母到最不常用字母的顺序排列。

我已经完成了一个工作 Java 程序的编码,该程序询问文件名并输出文件中的文本。但我不确定如何输出字母列表。我不确定具体是我可以使用读取器类中的哪些方法(如果有)读取 .txt 文件中的每个字母。任何帮助将不胜感激!

这是当前代码:

// Here I import the Bufered Reader and file reader Libraries
// The Buffered Reader library is similar to Scanner Library and 
// is used here to read from a text file. File reader will allow
// the program to access windows file system, get the text file
// and allow the Buufered Reader to read it in.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class TextFileReaderApp
{
    // I added "throws exception" in case there is an an error in the
    // main method, throw an exception, so it can prevent further
    // errors from occuring if java doesnt know the main methods going
    // to throw an error.
    public static void main(String[] args) throws Exception
    {
        // below I diplay a welcome messgae to the user
        System.out.println();
        System.out.println("Welcome to the Text File Reader application!");
        System.out.println();

        // Below I create an instance of the Scanner class to get
        // input from the user.
        Scanner userInput = new Scanner(System.in);
        String selection = "y"; //this is the string variable that's used in
                                //the while loop to continue the program.

        // Below I created a while loop that continues the program if the user
        // keeps selecting y as their selecion
        while (selection.equalsIgnoreCase("y"))
        {
            // this line of code is supposed to ask the user for text file name under
            // the C:/ directory and must not be hidden in any foler.
            System.out.print("Please enter the name of the .txt file: C/");
            FileReader file = new FileReader("C:/" + userInput.next());

            // file object is used as a parameter in buffered reader.
            BufferedReader textReader = new BufferedReader(file);

            // below I create and initialize an object of type string called text that will
            // store whats inside of the text file.
            String text = "";

            // I use the readLine statement to read line after line of the text.
            // Once it has read everything it will return null.
            String lineText = textReader.readLine();

            // code below is a test for me to see if the code above works and is able to read
            // the text inside the file and output it.
            while(lineText != null)
            {
                // this reads the text line for line and ads it to the text variable for output.
                text = text + lineText + "\n";
                lineText = textReader.readLine();
            }
            System.out.println(text);
        }
        // These 3 code lines ask the user if he/she would like to continue with the program.
        System.out.println();
        System.out.print("Continue using the Text File Reader? (y/n): ");
        choice = user_input.next();
        System.out.println();
    }
}

【问题讨论】:

  • 你会想要阅读java中Reader类的javadoc。
  • 这是一系列要求我们为您编写代码的语句,而不是实际问题。
  • 不,我不希望任何人为我编写代码,但我只是想知道阅读器类中可以读取文本中单词字母的方法。我可以处理除此之外的所有事情,但我只是想澄清一下。
  • 您不想使用textReader.read(),请参阅我的答案作为示例:) 希望对您有所帮助,如果您需要其他说明,请写。

标签: java text-files bufferedreader filereader


【解决方案1】:

如果您需要计算字母/字符,您也可以在行/单词等上进行计算。此处无需让读者参与。

 for (char c : someString.toCharArray ()) {
     // handle the character
  }

一旦你的文件中有任何字符串,你应该可以工作。

【讨论】:

    【解决方案2】:

    这会读取来自textReader 的所有字符,直到到达 EOF 或发生异常。

    try { for(int i = textReader.read(); i != -1 /* EOF */; i = textReader.read()) { char c = (char) i; // do whatever you want with your char here } } catch(IOException) textReader.close();

    【讨论】:

      【解决方案3】:

      首先,您可能希望使用 StringBuilder 而不是您的 String 文本,因为性能要好得多。 “text = text + lineText”每次执行都会创建另一个String对象,StringBuilder在这种情况下效果更好)。

      实现您想要的一种方法是逐个字符地读取 textLine 的字符,并使用带有所有字母的 switchcase 块,并在它们出现时将它们添加到包含整数的数组中。示例:

      int[] array = new int[26];
      switch(character){
      case "a": 
          array[0] += 1;
          break;
      case "b": 
          array[1] += 1;
          break;
      //....
      }
      

      等等…… 最后,您使用一个简单的 for 循环并打印数组的值。现在你看看你输入了多少次哪个字符。

      【讨论】:

      • 感谢您的意见!所以根据我的理解,我必须为每个字母编写代码,将数组元素的计数增加一?我可以创建一个包含 26 个元素的数组,并且每次读取一个字符时将某个元素递增 1。但是有一个问题,我可以使用 if/else if 语句代替 switchcase 块吗?我对使用 switchcase 块不太熟悉,我对 java 还是很陌生。
      • 是的,这就是我的建议。当然你也可以用 if/else 块来做,但是使用 switch/case 看起来更干净,更容易阅读。你可能想看看这个页面:docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多