Yes. It is an ordinary text file. On Unix, use the cat command. Or use any text editor.
print()
and println()
import java.io.*; class PrintSquare { public static void main ( String[] args ) throws IOException { File file = new File( "mySquare.txt" ); PrintWriter output = new PrintWriter( file ); double x = 1.7320508; output.println( "The square of " + x + " is " + x*x ); output.close(); } }
The print(String s)
method of PrintWriter
sends the characters from the String
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
just as we have been doing with
System.out.println()
.
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:\temp> javac PrintSquare.java C:\temp> java PrintSquare C:\temp> 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 look at the file it creates.
Could the name of the output file come from user input?