Yes. It is an ordinary text file. On Unix, use the cat command. Or use any text editor.
print()
and println()
The print(String s)
method of PrintStream
sends the characters referenced by s
to the output stream.
No line separator characters are sent after them.
The println(String s)
method
sends the same characters
followed by line separator characters.
Both methods can be used to output numerical results as characters
the same way we have been doing with
.
Here is a program that does this:
import java.io.*; class PrintSquare { public static void main ( String[] args ) throws IOException { File file = new File( "mySquare.txt" ); PrintStream print = new PrintStream( file ); double x = 1.7320508; print.println( "The square of " + x + " is " + x*x ); print.close(); } }
The following
"The square of " + x + " is " + x*x
creates one long string by converting x
and x*x
to character form and then appending those characters to the string literals.
It is this final long string that is the argument to println()
.
Here is a run of the program:
C:\> javac PrintSquare.java C:\> java PrintSquare C:\> type mySquare.txt The square of 1.7320508 is 2.9999999737806395
The program does not send any output to the command window. To see the output of the program you have to look at the file it creates.
Could the name of the output file come from user input?