I often use the Scanner class to read files because it is so convenient.
String inputFileName;
Scanner fileScanner;
inputFileName = \"inp
In ACM Contest the fast read is very important. In Java we found found that use something like that is very faster...
FileInputStream inputStream = new FileInputStream("input.txt");
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader in = new BufferedReader(streamReader);
Map map = new HashMap();
int trees = 0;
for (String s; (s = in.readLine()) != null; trees++) {
Integer n = map.get(s);
if (n != null) {
map.put(s, n + 1);
} else {
map.put(s, 1);
}
}
The file contains, in that case, tree names...
Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
You can use the StringTokenizer
for catch any part of line that your want.
We have some errors if we use Scanner
for large files. Read 100 lines from a file with 10000 lines!
A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.
tells in the API
Good luck!