IO: ClassicIoManBuffered
Buffered Manually
This commit is contained in:
44
ClassicIoManBuffered/src/ClassicIoManBuffered.java
Normal file
44
ClassicIoManBuffered/src/ClassicIoManBuffered.java
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user