Skip to content

Instantly share code, notes, and snippets.

@evoxtorm
Created April 9, 2017 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evoxtorm/0d7ac395dec650e05146442dbdc0461d to your computer and use it in GitHub Desktop.
Save evoxtorm/0d7ac395dec650e05146442dbdc0461d to your computer and use it in GitHub Desktop.
code for reading a text file and returning it's length.
import java.io.*;
public class Test {
public static void main(String [] args) {
String fileName = "abc.txt";
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
System.out.println("total length of the file :" + fileName.length());
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment