Reading a file using Java's Scanner class

Caution

I am new to writing in Qitta. In addition, since I am a Hiyokko university student, I would appreciate it if you could read it considering that there is a great possibility that mistakes may be included.

Introduction

This article is a personal summary of the Scanner class that comes standard with Java. is there. As for the background of writing this article, → The specification said that it should be made using the Scanner class. → I don't know that. → I was able to do it. → Make a note somewhere so that you don't forget it. It's like that. By the way, I usually use BufferedReader or FileReader /javase/jp/8/docs/api/java/io/FileReader.html) is used. (Yes, here ...)

Read the file

FileReader


        try {
            Scanner scanner = new Scanner (new File ("sourceFileName"/*File name you want to read here(In some cases you also need a pass)*/));
        } catch (FileNotFoundException e) { //exception
            //What to do when the file does not exist
            e.printStackTrace();
        }

The part that reads the file for the time being. When creating an object, insert the object of File class as an argument. The File class retains the file information when an object is created with the file path as an argument. Convenient. I think you should do it with the constructor.

Read line by line from Scanner

ReadLine


        try {
            //When the file has unread parts
            if (scanner.hasNextLine()) { 
                line = scanner.nextLine() + '\n';                                                      
            //When not
            } else {
                line = null;
            }
        //Read error
        } catch (IllegalStateException e) { 
            e.printStackTrace();
            closeFile();
            System.exit (1);
        }
Method name motion
hasNextLine() Returns true if the scanner has the following line
nextLine() Advance the scanner and return a line

line is a String type variable and is held as a field this time. If you output this line, the contents of the file will be output safely. "\ N" is added for output. I think you can start a new line in the main statement.

Read character by character from Scanner

ReadChar


        current = next;
        if (next == '\0') {
        //Only common operation at the end of the file
        } else if (next == '\n') {
            //End of line
            readLine();
            if (line != null) {
                //The line read is not null
                next = line.charAt (0);
                number = 0;
            } else {
                //The line read is null(End of file)in the case of
                next = '\0'; //'\0'Represents the end of the file
            }
        } else {
            //If it is neither the end of the file nor the end of the line
            next = line.charAt (++number);
        }
        return current;
Variable name motion
current Line being read
next Next line to read
number Current number of characters

I made it using the readLine mentioned above. Operationally, it cuts out what was read line by line, character by character. To be honest, it's unreasonable, but there are no specifications because the specifications are like this.

Finally

I made it so that it would work for the time being. All that is left is to output with a while statement or the like. In conclusion, it's annoying. Limited to that. I just hope that future students in a similar situation will not have a hard time. If you have any problems, first read the official API documentation (English)!

Recommended Posts

Reading a file using Java's Scanner class
[Personal memo] Reading a file using BufferedReader
Create a static file that expands variables using the ERB class
[Personal memo] Writing a file using BufferedWriter
java Scanner class
Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
A story packed with Java's standard input Scanner
Getting a little stuck on the last day of the month using Java's Calendar class
When using the constructor of Java's Date class, the date advances by 1900.
Test a class that takes a Context and reads / writes a file