Friday, December 8, 2017

JAVA - How read text file

Rows are retrived row by row from text file with helping of java.io.BufferedReaded class.
import java.io.*;
...
/* -- 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 ) {}
Output could be:
50
51,9940881520819
53,9831973101194
55,9623857448431
57,92678579208
59,8716397322638
...

No comments:

Post a Comment