Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to read a text file using Scanner in Java?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 777
    Comment on it

    Hello!!

    As we know that their are multiple way to read the text file in java. BufferedReader class is generally uses to read the text file. FileReader, BufferedReader, FileInputStream is used to reade the text file in java depending on type and requirement of the file. Scanner class is mostly used for the reading user input but we can use the Scanner class to read the text file in java. Scanner class also provides the parsing function, with the help of the parsing function we can parse the file data into correct data type. Scanner is used to read the file line by line. We have nextLine() to return the next line of the file. nextInt(), nextFloat() are used to parse it in correct data type.

    The java.util.Scanner is added on java 1.5 version so many of the devices  are able to support these functionality of the scanner class.

    Example:-

    public static void readTextFileUsingScanner(String file) {
        try {
          Scanner sc = new Scanner(new File(file));
          while (sc.hasNext()) {
            String str = sc.nextLine();
            System.out.println(str);
          }
          sc.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    

    In the above example, We have used the hasNext() metod for checking the end of the file and  nextline() method is used to read it line by line. 

    Let us suppose that their is a file called file.txt. It contains some text. We can read the file line by line and also word by word. We uses the next() method to read the file line by line.

    Code:-  Here is the code for reading file.txt line by line and word by word.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    public class ScannerFR {
    
      public static void main(String[] args) throws Exception {
    
        // reading a text file line by line using Scanner
        System.out.println("Reading a text file line by line: ");
        Scanner sc = new Scanner(new File("file.txt"));
        while (sc.hasNext()) {
          String str = sc.nextLine();
          System.out.println(str);
        }
        sc.close();
    
    
        // reading all words from file using Scanner
        System.out.println("Reading a text file word by word: ");
        Scanner sc2 = new Scanner(new File("file.txt"));
        while (sc2.hasNext()) {
          String word = sc2.next();
          System.out.println(word);
        }
    
        sc2.close();
    
      }
    }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: