JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to read file content line by line in java?


Description:

Below example shows how to read file content line by line. To get this, you have to use BufferedReader object. By calling readLine() method you can get file content line by line. readLine() returns one line at each iteration, we have to iterate it till it returns null.


Code:
package com.java2novice.files;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadLinesFromFile {

	public static void main(String a[]){
		BufferedReader br = null;
		String strLine = "";
		try {
			br = new BufferedReader( new FileReader("fileName"));
			while( (strLine = br.readLine()) != null){
				System.out.println(strLine);
			}
		} catch (FileNotFoundException e) {
			System.err.println("Unable to find the file: fileName");
		} catch (IOException e) {
			System.err.println("Unable to read the file: fileName");
		}
	}
}
<< Previous Program | Next Program >>
blog comments powered by Disqus

Java File IO Operations Sample Code Examples

  1. All file operations.
  2. Show list of all file names from a folder.
  3. How to get list of all files from a folder?
  4. Filter the files by file extensions and show the file names.
  5. How to read file content using byte array?
  6. How to read file content line by line in java?
  7. How to read property file in static context?
  8. How to read input from console in java?
  9. How to get file list from a folder filtered by extensions?
  10. How to get file URI reference?
  11. How to store and read objects from a file?
  12. How to create and store property file dynamically?
  13. How to store property file as xml file?
  14. How to get file last modified time?
  15. How to convert byte array to inputstream?
  16. How to convert inputstream to reader or BufferedReader?
  17. How to convert byte array to reader or BufferedReader?
  18. How to set file permissions in java?
  19. How to read a file using BufferedInputStream?
  20. How to create temporary file in java?
  21. How to write or store data into temporary file in java?
  22. How to delete temporary file in java?
  23. How to write string content to a file in java?
  24. How to write byte content to a file in java?
Knowledge Centre
wait Vs sleep methods
sleep(): It is a static method on Thread class. It makes the current thread into the "Not Runnable" state for specified amount of time. During this time, the thread keeps the lock (monitors) it has acquired.

wait(): It is a method on Object class. It makes the current thread into the "Not Runnable" state. Wait is called on a object, not a thread. Before calling wait() method, the object should be synchronized, means the object should be inside synchronized block. The call to wait() releases the acquired lock.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.