Skip to content

Instantly share code, notes, and snippets.

@MrCoder
Last active December 19, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrCoder/5934250 to your computer and use it in GitHub Desktop.
Save MrCoder/5934250 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/3122422/usage-of-bufferedinputstream The following code support the selected answer on stackoverflow.
String sourceFileName = "source.ged";
String userDir = System.getProperty("user.dir");
File sourceFile = new File(userDir, sourceFileName);
@Test // 1s
public void read_with_FileInputStream() throws IOException {
for (int i = 0; i < 1000; i++){
FileInputStream fileInputStream = new FileInputStream(sourceFile);
int read = fileInputStream.read();
while (read != -1){
read = fileInputStream.read();
}
fileInputStream.close();
}
}
@Test // 0.029s
public void read_with_FileInputStream_read_800_bytes_each_time() throws IOException {
for (int i = 0; i < 1000; i++){
FileInputStream fileInputStream = new FileInputStream(sourceFile);
byte[] bytes = new byte[800];
int read = fileInputStream.read(bytes);
while (read == 800){
read = fileInputStream.read(bytes);
}
fileInputStream.close();
}
}
@Test // 0.093s
public void read_with_FileInputStream_and_BufferedInputStream() throws IOException {
for (int i = 0; i < 1000; i++){
InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
int read = fileInputStream.read();
while (read != -1){
read = fileInputStream.read();
}
fileInputStream.close();
}
}
@Test // 0.043s
public void read_with_FileInputStream_and_BufferedInputStream_read_800_bytes_each_time() throws IOException {
for (int i = 0; i < 1000; i++){
InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
byte[] bytes = new byte[800];
int read = fileInputStream.read(bytes);
while (read == 800){
read = fileInputStream.read(bytes);
}
fileInputStream.close();
}
}
@Utumno
Copy link

Utumno commented Jul 27, 2013

Would taking InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile)); out of the for loop make a difference ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment