简体   繁体   English

如何从java中的文本文件中读取您的输入

[英]How to read your input from a text file in java

I'm sort of new to java and i have this lexical analyzer program for my assignment, the program works fine and my only problem is my input needs to come from a text file.我对java有点陌生,我有这个词法分析器程序用于我的作业,该程序运行良好,我唯一的问题是我的输入需要来自文本文件。

public class LexicalAnalyzer {

public static void main(String[] args) {

   Scanner reader = new Scanner(System.in);
   System.out.println("Input strings: ");
   String str = reader.nextLine();

   for (int i = 0; i < str.length(); i++)
   {
       switch (str.charAt(i))
       {
           case '+':
                System.out.println("Token: +   Type: ADDITION");
                break;
           case '-':
                System.out.println("Token: -   Type: SUBTRACTION");
                break;
           case '*':
                System.out.println("Token: *   Type: MULTIPLICATION");
                break;
           case '/':
                System.out.println("Token: /   Type: DIVISION");
                break;
           case '(':
                System.out.println("Token: (   Type: LEFT PARENTHESIS");
                break;
           case ')':
                System.out.println("Token: )   Type: RIGHT PARENTHESIS");
                break;
           case '=':
                System.out.println("Token: =   Type: EQUALS");
                break;
           case ';':
                System.out.println("Token: ;   Type: END");
                break;
           case '0':
                System.out.println("Token: 0   Type: NUM");
                break;
           case '1':
                System.out.println("Token: 1   Type: NUM");
                break;
           case '2':
                System.out.println("Token: 2   Type: NUM");
                break;
           case '3':
                System.out.println("Token: 3   Type: NUM");
                break;
           case '4':
                System.out.println("Token: 4   Type: NUM");
                break;
           case '5':
                System.out.println("Token: 5   Type: NUM");
                break;
           case '6':
                System.out.println("Token: 6   Type: NUM");
                break;
           case '7':
                System.out.println("Token: 7   Type: NUM");
                break;
           case '8':
                System.out.println("Token: 8   Type: NUM");
                break;
           case '9':
                System.out.println("Token: 9   Type: NUM");
                break;   
       }
   }      

Any ideas on how i can read my input from a text file?关于如何从文本文件中读取输入的任何想法?

You can use below utility methods from File class to read file contents.您可以使用 File 类中的以下实用程序方法来读取文件内容。

Files.readAllLines() or Files.lines() Files.readAllLines()Files.lines()

Look at this https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/ it's so easy看看这个https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/就是这么简单

read the file as lines and split every line to accomplish your mession将文件作为行读取并拆分每一行以完成您的任务

there are many ways to read from file such as using buufererdReader as example有很多方法可以从文件中读取,例如使用 buufererdReader 作为示例

i think it's an easy question you should do some search before asking any question on stackoverflow我认为这是一个简单的问题,您应该在询问有关 stackoverflow 的任何问题之前进行一些搜索

hope it ll be useful for you希望对你有用

I am giving you the program for reading from text file.我正在为您提供从文本文件中读取的程序。

Here is the code这是代码

public class ReadToFile {

    public static void main(String[] args) {
        try {
            //File file = new File("C:\\Sample\\ketan.txt");
            FileReader fileReader = new FileReader("C:\\Sample\\xyz.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String content = bufferedReader.readLine();
            System.out.println("Content of file is " + content);
            bufferedReader.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM