http://www.technicalpage.net/search/label/SQL

ReadTxtFile

 Read txt file


package Test;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

 

public class ReadTextFile {

 

       public static void main(String[] args) throws Exception {

            

             File file = new File("C:\\.... \\ReadFile.txt");

                   

             Scanner scr = new Scanner(file);

             String words = "" ;

             while(scr.hasNext()){

                    words = scr.nextLine();

                    System.out.println(words);

             }           

       }

 

}

 

Output:

This is right.

This is also right.

Hello, good morning.

 

Life is beautiful. People say different things about life. But life should be taken easily. But at the same time it is very complex.

 

Google is the source of a lots of information. You can depend on it for any kind of information. Nowadays, right information at right time is essential.

 if we use words = scr.next(); instead of words = scr.nextLine(); in above code, we get below result:

This
is
right.
This
is
also
right.
Hello,
good
morning.
Life
is
beautiful.
People
say
different
things
about
life.
But
life
should
be
taken
easily.
But
at
the
same
time
it
is
very
complex.
Google
is
the
source
of
a
lots
of
information.
You
can
depend
on
it
for
any
kind
of
information.
Nowadays,
right
information
at
right
time
is
essential.


No comments:

Post a Comment