How to read file in Java – BufferedReader

In Java, there are many ways to read a file, here we show you how to use the simplest and most common-used method – BufferedReader.

package com.mkyong.io;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class BufferedReaderExample {
 
	public static void main(String[] args) {
 
		BufferedReader br = null;
 
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader("C:\\testing.txt"));
 
			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
 
	}
}

See updated example in JDK 7, which use try-with-resources new feature to close file automatically.

package com.mkyong.io;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class BufferedReaderExample {
 
	public static void main(String[] args) {
 
		try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt")))
		{
 
			String sCurrentLine;
 
			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} 
 
	}
}
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: water ionizer loans()

  • Pingback: car parking()

  • Pingback: water ionizer machine()

  • Pingback: mp4 mobile porn()

  • Pingback: laan penge nu og her()

  • Pingback: parking()

  • Pingback: her og nu lan()

  • Pingback: get satellite tv()

  • Pingback: best online casinos()

  • Pingback: Blue Coaster33()

  • priya

    can anyone say how to write a java code for wordnet that it should read the text file and display the synonyms for each word.

  • http://www.javadiscover.com Anand Kumar

    can get complete file operations using java code here – http://www.javadiscover.com/search/label/File

  • Jens Preem

    What if when I want to read in file not from newline to newline but from some other token to token? Like from LABEL to LABEL etc. What tool should I use then?

    • prestige

      i guess u have to use Scanner to read the file.

      public class readFile
      {
      public static void main(String args[])
      {
      File f = new File(filePath);
      Scanner read = new Scanner(f).useDelimeter(“put anything as a delimeter eg: ##”);
      String content =read.next();
      while((content=read.next()).hasNext())
      {
      content += content;
      }
      }
      }

  • http://www.worldbestlearningcenter.com Dara

    Itis a very good code. I alos have written some tips in Java for file access and manipulation including reading, writing, creating file, and folder,…http://www.worldbestlearningcenter.com/tips/Java-write-file.htm

  • Pingback: Reading reading a text file in Java | Java Programming()

  • Jane

    HE HE HE HI

  • CJ

    thanks you !

  • Raja

    Hi, your codes works fine but i am getting an encrypted version, so how can i get non-encrypted version.

  • Dominik

    Hi.

    Thank you.

    Good bye.

    • Jane

      Hi, Goodbye, Thankyou, what the heck???
      :( otherwise known as :(((

      • Nick

        It is a really weird saying, I have to agree with that.

  • Dam

    How can you adapt the code to allow the variable “sCurrentline”, to be manipulated through methods such as, .split(), out side of the try-catch statement. It will not even print “sCurrentLine”, unless it is in side the try-catch statement.

  • Fernando

    Hi, where the txt file should be placed??
    I created a txt file on the same folder of the java files and the application always throw the FileNotFoundException. I tried different names, different format files, but it never finds the file.

    Thanks

    • srija

      you can save it anywhere but you have to save it with .java extension

  • http://antguider.blogspot.com Muthukumar JeyaMurugan

    The entire Java File Operation like Reading, writing, Delete are found here,
    http://antguider.blogspot.in/2012/06/java-file-operation.html

  • http://www.worldcommunitygrid.org/ Peter

    Finally after much searching, you answered my question, Mike !
    Thanks !
    There are a lot of people out in the web asking the same question.
    When they and, (up till a few moments ago), myself included, try to “import” a simple text file into an Eclipse project, we were all receiving “file not found”.
    Being new to Java I was amazed that this was such an “issue” for eclipse. I like Eclipse but something so fundamental, shouldn’t be so frustratingly hard. I guess the Devs are busy working on other more urgent issues and we cant complain as it is a free and in reality a good product.
    Many thanks ! (c:\\testing) :)

  • http://www.mkyong.com aljun

    somebody help this problem …
    how to read this file 1
    23
    45

  • Neo

    In your example, you should make sure to close the BufferedReader, otherwise the file may be lock not readable by some other process.

    so
    ….
    while ((sCurrentLine = br.readLine()) != null) {
    System.out.println(sCurrentLine);
    }

    br.close();
    …..

    • http://www.mkyong.com mkyong

      Example is updated, with new JDK7 example.

      • priya

        sir i want jtree tool coding for netbeans 6.9

        • Jane

          He he HI!

          • http://addggnhn muhammad adil

            great work done by grat man

      • Ivan

        Excelente (Y)

  • Pingback: How to read file from Java - BufferedInputStream Example | Java()