Java writer example

Example below demonstrates working of abstract writer class. Java writer is an
abstract class build inside the java.io package. In the example several
class methods of the writer class are demonstrated.
In the example, the java writer subclasses classes, FileWriter & PrintWriter are
used to write string and other data as text characters in file rose.txt. Java
writer class is used to write text files therefore as the classes of
abstract writer class is used, a character output stream forms.
Now with the
class methods, string and number type data is written in to the text
files.
Note: While using the classes of abstract writer class, an output
stream is formed because we are flowing data as characters from the internal
memory of an application to a disk file.
Java_writer_example.java
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class Java_writer_example {
public static void main(String args[]) throws IOException {
File file = new File("rose.txt");
// file.createNewFile();
FileWriter txt = new FileWriter(file);
PrintWriter out = new PrintWriter(txt);
out.append("Welcome to roseindia.net\n");
char ch[] = { 'n', 'e', 'w', 's', 't', 'r', 'a', 'c', 'k', 'i', 'n',
'd', 'i', 'a', '.', 'c', 'o', 'm' };
out.print("\n\tWelcome to ");
out.write(ch);
out.close();
}
}
|
Download the code

|