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();
}
}
}
}
I think you only need to do the dis.close(), it will automatically closes all the under layer stream such as BufferredInputStream and FileInputStream
How do i go about reading from a word document?
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.
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.”
following website gives more excellent examples on core java, c, etc
http://www.codesnippets4all.com
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.
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
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.
There are 4 different ways to read file in java, http://www.journaldev.com/867/how-to-read-file-in-java-using-bufferedreader-scanner-files-with-encoding-support-and-filereader
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.
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.
@JEN Request you to start a Blog like this and then you can compare with MKYong.
@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.