how-to

File & Directory

How to read data from text file

For this reading from text file you need:

import java.io.*;

Rows are retrived row by row from text file with helping of java.io.BufferedReaded class.


/* -- check if file exists */

File file = new File("c:\\test2.txt");

if (!file.exists()) {
  return;
}

/* -- read data by rows */

try {
  BufferedReader in = new BufferedReader( new FileReader( file ) );

  try {
    String s;
    while ( ( s = in.readLine() ) != null ) {

      /* action with row */

      System.out.println( s );
    }
  } finally {

    /* -- close reader */
 
    in.close();
  }
} catch ( IOException e ) {
}
The output is (when file exists):
50
51,9940881520819
53,9831973101194
55,9623857448431
57,92678579208
59,8716397322638