Last active
December 19, 2015 09:39
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would taking
InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
out of the for loop make a difference ?