Duke, the Java Mascot, in the waving pose. Duke images are now Free Graphics see duke:Project Home Page for more details. (Photo credit: Wikipedia) |
In this post I am sharing a code on how to read character files line by line for Java version 6 or less. The current java version being 8 and the way to read the files is a bit different, like we use Java NIO primarily. Still for the sake of knowledge this post will help you clear some concepts.
The following source shows you the way to read a file in Java version 6 or less (compatible with JDK 5 but not 4)
It covers most of the error handling cases possible and note that we should close BufferedReader when not in use. The following prints the time taken by the code to read the file and total number of the contents read (In this case I read a file containing all the words of a dictionary)
It covers most of the error handling cases possible and note that we should close BufferedReader when not in use. The following prints the time taken by the code to read the file and total number of the contents read (In this case I read a file containing all the words of a dictionary)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package files; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
/** | |
* | |
* @author psycho_coder | |
*/ | |
public class ReadFileJava6Style { | |
private BufferedReader br; | |
private ArrayList<String> list = null; | |
public ReadFileJava6Style() { | |
br = null; | |
list = new ArrayList<>(); | |
} | |
private int getSize() { | |
return list.size(); | |
} | |
private void readFile(File file) throws FileNotFoundException, IOException { | |
if (file == null) { | |
throw new IllegalArgumentException("File shouldn't be Null"); | |
} | |
if (!file.exists()) { | |
throw new FileNotFoundException("The given file was not found"); | |
} | |
if (!file.isFile()) { | |
throw new IllegalArgumentException("Supplied parameter is a directory and not a file."); | |
} | |
if (!file.canRead()) { | |
throw new IllegalArgumentException("You Don't have permission to read the file"); | |
} | |
try { | |
br = new BufferedReader(new FileReader(file)); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
list.add(line); | |
} | |
} catch (IOException ex) { | |
System.err.println("IO error when reading file." + ex.getMessage()); | |
} finally { | |
if (br != null) { | |
br.close(); | |
} | |
} | |
} | |
public static void main(String... args) throws IOException { | |
File file = new File(System.getProperty("user.dir") + "/src/data/en"); | |
long time = System.nanoTime(); | |
ReadFileJava6Style readfile = new ReadFileJava6Style(); | |
readfile.readFile(file); | |
System.out.println(System.nanoTime() - time); | |
System.out.println("No. of Items read : " + readfile.getSize()); | |
} | |
} |
Related articles