INPUT AND OUTPUT
Input/Output is not as complicated as it looks. We can get pretty far just writing to System.out (which is of type PrintStream ) with methods print , println , and printf . The method print simply writes converts its argument to a String and writes it to the output stream. The method println is similar, but adds a newline so that following output starts on a new line. The method printf is new in Java 1.5. It expects a String as its first argument and zero or more additional arguments. Each '%' in the first argument indicates a request to print one of the other arguments. The details are spelled out by one or more characters following the '%'. For example, out.printf("pair(%d, %d)%n", pair.x, pair.y);
produces exactly the same thing as out.println("pair(" + pair.x + "," + pair.y + ")");
but is much easier to read, and to write. The characters %d are replaced by the result of converting the next argument to a decimal integer. Similarly, "%f" looks for a float or double, "%x" looks for an integer and prints it in hexadecimal, and "%s" looks for a string. Fancier formatting is supported; for example, "%6.2f" prints a float or double with exactly two digits following the decimal point, padding with leading spaces as necessary to make the result at least 6 character long.
For input, we probably want to wrap the standard input System.in in a BufferedReader , which provides the handy method readLine()
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
for(;;) {
String line = input.readLine();
if (line == null) {
break;
}
// do something with the next line
}
If We want to read from a file, rather than from the keyboard (standard input), we can use FileReader, probably wrapped in a BufferedReader.
BufferedReader input =
new BufferedReader(new FileReader("somefile"));
for (;;) {
String line = input.readLine();
if (line == null) {
break;
}
// do something with the next line
}
Similarly, we can use new PrintWriter(new FileOutputStream("whatever")) to write to a file.
produces exactly the same thing as out.println("pair(" + pair.x + "," + pair.y + ")");
but is much easier to read, and to write. The characters %d are replaced by the result of converting the next argument to a decimal integer. Similarly, "%f" looks for a float or double, "%x" looks for an integer and prints it in hexadecimal, and "%s" looks for a string. Fancier formatting is supported; for example, "%6.2f" prints a float or double with exactly two digits following the decimal point, padding with leading spaces as necessary to make the result at least 6 character long.
For input, we probably want to wrap the standard input System.in in a BufferedReader , which provides the handy method readLine()
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
for(;;) {
String line = input.readLine();
if (line == null) {
break;
}
// do something with the next line
}
If We want to read from a file, rather than from the keyboard (standard input), we can use FileReader, probably wrapped in a BufferedReader.
BufferedReader input =
new BufferedReader(new FileReader("somefile"));
for (;;) {
String line = input.readLine();
if (line == null) {
break;
}
// do something with the next line
}
Similarly, we can use new PrintWriter(new FileOutputStream("whatever")) to write to a file.
No comments:
Post a Comment