Skip to content

Instantly share code, notes, and snippets.

@diegogaulke
Last active August 29, 2015 14:05
Show Gist options
  • Save diegogaulke/499ad54fcaa679b6df9f to your computer and use it in GitHub Desktop.
Save diegogaulke/499ad54fcaa679b6df9f to your computer and use it in GitHub Desktop.
Read data from a text file with Java
package com.dg2ee.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:\\test.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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment