Java file handling tutorial - Reading from a file


Home - Tutorials - File handling

This tutorial demonstrates you the basic Java file handling concepts with code examples.

Tutorial info:


Name:Java file handling tutorial
Total steps:2
Category:File handling
Date:2009-04-27
Level:Beginner
Product:See complete product
Viewed:104396

Bookmark Java file handling tutorial



AddThis Social Bookmark Button

Step 2 - Reading from a file


Java file handling tutorial


If everything is alright, you have successfully written some basic text into  a file. Now it's time to read the file content back. Not surprisingly reading from a file is very similar to writing. We only need to use *Reader objects instead of *Writer objects. It means that you can use FileReader or BufferedReader. As a simple FileReader can handle only a single character or a character array it is more convenient to use the BufferedReader which can read a complete line from a file as a string. So using a BufferedReader we can read a text file line by line with the readln() method as you can see in this example:
Code:
  1. public String readFile() {
  2. String fileName = "c:\\test.txt";
  3. String LS = System.getProperty("line.separator");
  4. StringBuffer fileContent = new StringBuffer();
  5.  
  6. try {
  7. FileReader fr = new FileReader(fileName);
  8. BufferedReader reader = new BufferedReader(new FileReader(fileName));
  9.  
  10. String line;
  11. while ((line = reader.readLine()) != null) {
  12. fileContent.append(line).append(LS);
  13. }
  14.  
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18.  
  19. return fileContent.toString();
  20. }

I hope this article made a beginner java programmer life easier. Finally let's see how the complete Java program looks like:
Code:
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6.  
  7. public class FileDemo {
  8.  
  9. public static void main(String[] args) {
  10. FileDemo fd = new FileDemo();
  11. fd.writeFile();
  12. String txt = fd.readFile();
  13. System.out.println(txt);
  14. }
  15.  
  16. public void writeFile() {
  17. String fileName = "c:\\test.txt";
  18.  
  19. try {
  20. BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
  21. writer.write("Test text.");
  22. writer.newLine();
  23. writer.write("Line2");
  24. writer.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. public String readFile() {
  31. String fileName = "c:\\test.txt";
  32. String LS = System.getProperty("line.separator");
  33. StringBuffer fileContent = new StringBuffer();
  34.  
  35. try {
  36. FileReader fr = new FileReader(fileName);
  37. BufferedReader reader = new BufferedReader(new FileReader(fileName));
  38.  
  39. String line;
  40. while ((line = reader.readLine()) != null) {
  41. fileContent.append(line).append(LS);
  42. }
  43.  
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47.  
  48. return fileContent.toString();
  49. }
  50. }
  51.  

 




Previous Step of Java file handling tutorial


Tags: java file handling tutorial, java file handling, file handling tutorial, java, file

Java file handling tutorial - Table of contents
Step 1 - Java file handling basics
Step 2 - Reading from a file



Total time: 0.168