Showing posts with label java - files. Show all posts
Showing posts with label java - files. Show all posts

Monday, January 22, 2018

JAVA - How to rename file

import java.io.*;
...
File f = new File( "c:\\new_name.txt" );

if ( f.exists() ) {

  /* -- here can be override question */

  f.delete();
}

boolean b = new File( "c:\\old_name.txt" ).renameTo( f );

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
...