Java IO Tutorial

How to read file in Java – BufferedInputStream

Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStream classes.

The readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader.

You may interest to read this How to read file from Java – BufferedReader


package com.mkyong.io;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamExample {

	public static void main(String[] args) {

		File file = new File("C:\\testing.txt");
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;

		try {
			fis = new FileInputStream(file);

			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);

			while (dis.available() != 0) {
				System.out.println(dis.readLine());
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				bis.close();
				dis.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
henry
8 years ago

I think you only need to do the dis.close(), it will automatically closes all the under layer stream such as BufferredInputStream and FileInputStream

James Jay-Jay Skhosana
9 years ago

How do i go about reading from a word document?

NISHIT PATIRA
9 years ago

You need to use an API. This is because, unlike a text file which is a flat file with no encryption or wrapper around it, Word documents are wrapped. So if you try to read from a word document using fileInputStream, you will get gibberish data.
I recommend using Apache POI for the purpose of reading from any Microsoft Office Applications.

hemant
8 years ago

Nice tutorial but dis.readLine() is deprecated so please update.

“This method does not properly convert bytes to characters. As of
JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine()
method. Programs that use the DataInputStream class to read
lines can be converted to use the BufferedReader class by
replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:

BufferedReader d
= new BufferedReader(new InputStreamReader(in));
See the general contract of the readLine method of DataInput.
Bytes for this operation are read from the contained input stream.”

Java Guru
9 years ago

following website gives more excellent examples on core java, c, etc

http://www.codesnippets4all.com

-DC-
11 years ago

I used a variation of this to read input from an HttpURLConnection on Android. It works, but the dis.available() method is not very reliable: “Note that this method provides such a weak guarantee that it is not very useful in practice.” So weak, in fact, that it returned 0 bytes available when data actually did exist.

A previous method I employed was to use InputStreamReader with BufferedReader to accomplish the same thing since BufferedReader also provides a readLine() method.

A
11 years ago

Thank you for all your perfect easy tutorials,
well, this for local file, what about relative path, i.e. when exporting code to Jar runnable file.

I googled it and tried all solution but still fail

This is one of solution which dosen’t work

  try {	
        	  
        	  InputStream is = getClass().getResourceAsStream("myTextFile.txt");
        	  if (is == null) 
        		  JOptionPane.showMessageDialog(null, "Resource not located.");
        	  else 
        		  JOptionPane.showMessageDialog(null, "Found why not work 1\n"+is);
        	  
          	InputStreamReader isr = new InputStreamReader(is,"UTF8");
  			 BufferedReader bf = new BufferedReader(isr);

when I run it using eclipse, it works fine, however when I run it using jar executable file, it shows me the message “Found why not working :(” which means getResourceAsStream get some values, but it dosen’t seem to read the file because I got no results as I would get using eclipse and the same input, any idea? thanks.

Pankaj
11 years ago
Reply to  A
A
11 years ago
Reply to  A

Thanks God. I already found the problem, it is because UTF8 encoding,the program actually read but not found anything because the charcter got is unknown like this ?. the solution if anyone interested.

 BufferedReader bf=  new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("myTextFile.txt"),"UTF8"));
Jen
11 years ago

readLine() is deprecated, and it has been for over a decade. Why intentionally mislead us with your horrific advice. After reading a few of your idiotic posts, I think you’re a Microsoft-shill that is trying to hurt Java.

Bala
10 years ago
Reply to  Jen

@JEN Request you to start a Blog like this and then you can compare with MKYong.

Deepak
10 years ago
Reply to  Jen

@Jen.Before commenting please go through the content of this website.compare it with any other website which provide tutorial on java.
I think solution presents here are clean,precise and easy to understand.