Count int in a file in Java
Description
The following code shows how to count int in a file.
Example
//w w w . j a v a 2 s . co m
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] arguments) {
try {
FileInputStream file = new FileInputStream("class.dat");
boolean eof = false;
int count = 0;
while (!eof) {
int input = file.read();
System.out.print(input + " ");
if (input == -1)
eof = true;
else
count++;
}
file.close();
System.out.println("\nBytes read: " + count);
} catch (IOException e) {
System.out.println("Error - " + e.toString());
}
}
}
The code above generates the following result.