Friday 19 August 2016

Java Tutorial : Java IO (Scanner class and bufferedreader)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=d9r_76YD_XM&list=UUhwKlOVR041tngjerWxVccw

myfile.txt
Welcome to India
Welcome to Japan
Welcome to USA
ScannerDemo.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/*
 *  Scanner reads file. 
 */
public class ScannerDemo
{

    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner scanner = null;
        try
        {
            FileReader fileReader = new FileReader("myfile.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            scanner = new Scanner(bufferedReader);

            while (scanner.hasNext())
            {
                String strValue = scanner.nextLine();
                System.out.println(strValue);
            }

        }
        finally
        {
            if (scanner != null)
            {
                /*
                 * Scanner's close method when it is done
                 * with the scanner object. Even though a
                 * scanner is not a stream, you need to
                 * close it to indicate that you're done
                 * with its underlying stream.
                 */
                scanner.close();
            }
        }
    }
}
Output
Welcome to India
Welcome to Japan
Welcome to USA

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Scanner_bufferedReader_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Scanner_bufferedReader_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/16638d65b46bec1088dab744ba69a31424c4a14f/BasicJava/JavaIODemo_Scanner_bufferedReader_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment