The character-oriented output streams are all subclasses of class writer, and their class hierarchy is shown in the figure.
The following table lists the main subclasses and descriptions of writers.
Writing files using the filewriter class
The filewriter class is a subclass of the writer subclass outputstreamwriter class,So the filewriter class can use either the methods of the writer class or the methods of the outputstreamwriter class to create objects.
When writing to a file using the filewriter class,You must first call the filewriter () constructor to create an object of the filewriter class,Then call the writer () method. The format of the filewriter constructor is:
public filewriter (string name);//Create a writable output stream object based on the file name
public filewriter (string name, boolean a);//a is true, data will be appended to the file
[Example] Use the filewriter class to write ascII characters to a file
import java.io. *;
class ep10_3 {
public static void main (string args []) {
try {
filewriter a=new filewriter ("ep10_3.txt");
for (int i=32;i<126;i ++) {
a.write (i);
}
a.close ();
}
catch (ioexception e) {}
}
}
After running the program,Open the ep10_3.txt file, the display content is:
! "#$%&" () * +,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz [\] ^ _ `abcdefghijklmnopqrstuvwxyz {|}
Writing to a file using the bufferedwriter class
The bufferedwriter class is used to write data to a buffer.You must create a filewriter class object when you use it, and then create an object of the bufferedwriter class with this object as a parameter.Finally you need to flush the buffer with the flush () method.The bufferedwriter class has two constructors,Its format is:
public bufferedwriter (writer out);//Create buffer character output stream
public bufferedwriter (writer out, int size);//Create an output stream and set the buffer size
[Example] Use the bufferedwriter class for file copying
import java.io. *;
class ep10_4 {
public static void main (string args []) {
string str=new string ();
try {
bufferedreader in=new
bufferedreader (new filereader ("ep10_4_a.txt"));
bufferedwriter out=new
bufferedwriter (new filewriter ("ep10_4_b.txt"));
while ((str=in.readline ())!=null) {
system.out.println (str);
out.write (str);//Write 1 line of data read to the output stream
out.newline ();//Write a newline character
}
out.flush ();
in.close ();
out.close ();
}
catch (ioexception e) {
system.out.println ("An error occurred" + e);
}
}
}
have to be aware of is,When calling the write () method of the out object to write data,No carriage returns are written,Therefore, you need to use the newline () method to add a carriage return after each line of data.To ensure that the target file is consistent with the source file.
Related articles
- Method for converting byte stream into character stream in java programming
- Example of mutual conversion function between Java implementation file and base64 stream
- Java implementation of input stream into String
- Java byte stream and basic data type conversion example
- Explain the difference between character stream and byte stream in Java
- Detailed interpretation of character-oriented input streams in Java programming
- Detailed description of java character stream buffer
- Byte and character stream IO operation example in Java programming
- Difference between Java character stream and byte stream for file operation
- Java character stream and byte stream difference and usage analysis
- Detailed explanation and examples of IO stream character stream in Java
- Java byte character conversion stream operation
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning