Changes

Jump to navigation Jump to search
880 bytes added ,  20:30, 3 April 2015
Line 341: Line 341:  
=== Reading text files ===
 
=== Reading text files ===
 
See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java]
 
See: [https://github.com/rafahsolis/javaTutorial/blob/master/ReadTextFile.java ReadTextFile.java]
 +
 +
=== Reading files with FileReader ==
 +
<source lang="java">
 +
import java.io.BufferedReader;
 +
import java.io.File;
 +
import java.io.FileNotFoundException;
 +
import java.io.FileReader;
 +
import java.io.IOException;
 +
 +
public class ReadingFiles {
 +
 +
public static void main(String[] args) {
 +
File file = new File("./src/tutorialJava/textFile.txt");
 +
BufferedReader br = null;
 +
 +
try {
 +
FileReader fr = new FileReader(file);
 +
br = new BufferedReader(fr);
 +
 +
String line;
 +
 +
while ((line = br.readLine()) != null) {
 +
System.out.println(line);
 +
}
 +
 +
} catch (FileNotFoundException e) {
 +
System.out.println("File not found" + file.toString());
 +
} catch (IOException e) {
 +
System.out.println("Unable to read the file: " + file.toString());
 +
} finally {
 +
try {
 +
br.close();
 +
} catch (IOException e) {
 +
System.out.println("Can't close the file.");
 +
}
 +
 +
}
 +
 +
}
 +
}</source>
    
== Anonymous class ==
 
== Anonymous class ==

Navigation menu