printWriter write2 Example
printWriter class write example. This example shows you how to use write method.
PrintWriter class write example.write() method in the program returns sequence of char values from the char array as per defined indexes.char array variable is passed in to the method constructor and the array value up to indexes defined is writed into the abstract pathname file.
Here is the code:-
/**
* @Program uses write method to generate sequence
* of char values as per defined indexes and to write to character
* output stream of specified abstract pathname file.
* Write2.java
* Author:-RoseIndia Team
* Date:-7-july-2008
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Write2 {
public static void main(String args[]) throws IOException {
File witches = new File("c:\tanushree.doc");
PrintWriter out = new PrintWriter(witches);
char[] array0 = { 'O', 'c', 'o', 'p', 's', 'O' }, array1 = { 's', 't',
'i', 'l', 'l', '0' }, array2 = { 'e', 's', 'c', 'a', 'p', 'e',
'd', '0' };
String str0 = new String("chasing"), str1 = "Thieves";
out.write(str1);
out.print(" ");
out.write(array2, 0, 7);
out.println("");
out.write(array0, 1, 4);
out.print(" ");
out.write(array1, 0, 5);
out.print(" ");
out.write(str0);
out.flush();
}
} |
Output of the program:-
Method generates results in the file specified and not on program output console.
Thieves escaped
cops still chasing |
|