IO: ClassicIoManBuffered

Buffered Manually
This commit is contained in:
Karsten Jeppesen
2021-04-12 21:31:10 +02:00
parent 4d3f031347
commit b06de215d7
4 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author KAJE
*
*/
public class ClassicIoManBuffered {
public static int lineCount(String path){
FileInputStream fis = null;
try {
fis = new FileInputStream( path );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte buf[] = new byte[2048];
int cnt = 0;
int n;
try {
while ((n = fis.read(buf)) != -1) {
for (int i = 0; i < n; i++) {
if (buf[i] == '\n') cnt++;
}
}
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cnt;
}
public static void main(String[] args){
long startTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lineCount("input.txt")));
long stopTime = System.nanoTime();
System.out.println("Time it took: " + (stopTime - startTime));
}
}