IO: ClassicIoWholeFile

Reading the whole file
This commit is contained in:
Karsten Jeppesen
2021-04-12 21:31:49 +02:00
parent b06de215d7
commit f2187a2b7f
4 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*
*/
/**
* @author KAJE
*
*/
public class ClassicIoWholeFile {
public static int lineCount(String path){
int len = (int)(new File(path).length());
int cnt = 0;
FileInputStream fis;
try {
fis = new FileInputStream( path );
byte buf[] = new byte[len];
try {
fis.read(buf);
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < len; i++) {
if (buf[i] == '\n') cnt++;
}
} catch (FileNotFoundException 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));
}
}