This is the 12th day of my participation in Gwen Challenge

Accumulate over a long period, constant dripping wears away a stone 😄

1, character output stream [Writer]

The java.io.Writer abstract class is a superclass that represents all classes used to write character streams, writing out the specified character information to the destination. It defines the basic public methods for the character output stream.

  • Void write(int C) : writes a single character.
  • Void write(char[] cbuf) : writes to the character array.
  • Void write(char[] cbuf, int off, int len) : Write a part of the character array, the starting index of the array off, the number of characters len wrote.
  • Void write(String STR) : writes a String.
  • Void write(String STR, int off, int len) : to write a part of the String, the index of the start of the String, the number of characters written by len.
  • Void flush() : Flushes the buffer for this stream.
  • Void close() : Close the stream, but refresh it first

1.1, FileWriter [File character output stream]

Java. IO. FileWriter extends OutputStreamWriter.

OutputStreamWriter extends Writer.

The OutputStreamWriter class comes later.

FileWriter: Writes memory data to a file.

1.1.1. Construction method

  • FileWriter(File File) : Creates a new oneFileWriter, given the File object to read.
  • FileWriter(String fileName) : Creates a new oneFileWriterGiven the name of the file to read.

When you create a stream object, you must pass in a file path, similar to FileOutputStream.

For example, the code is as follows:

public static void main(String[] args) throws IOException { 
        // Create a stream object with the File object
        File file = new File("a.txt"); 
        FileWriter fw = new FileWriter(file); 
        // Create a stream object with the file name
        FileWriter fw2 = new FileWriter("b.txt"); 
}
Copy the code

The function of the construction method:

  • 1. Create oneFileWriterobject
  • 2. Create a file based on the file/file path passed in the constructor
  • 3, will takeFileWriterThe object points to the created file

11.2 write out the data

  • Write a single character:write(int b)Method to write out one character at a time.

Code usage demo:

public static void main(String[] args) throws IOException { 

    // Create a stream object with the file name
    FileWriter fw = new FileWriter("IO stream / / which xt");
    // Write data to the memory buffer using the FileWriter method write.
    fw.write(98);
    fw.write('b');
    fw.write((30000));
    // Use the FileWriter method flush to flush data from the memory buffer to the file
    // Streams can also be used after the flush method
    fw.flush();
    // Release the resource (the data in the memory buffer is flushed to the file first)
    // The stream cannot be used again after using the close methodfw.close(); } Result: BB fieldCopy the code

Close and refresh

  • Flush: Flushes the buffer so that the stream object can continue to be used.

  • Close: Flush the buffer first and then inform the system to release the resource. The stream object can no longer be used.

  • Write (char[] cbuf) and write(char[] cbuf, int off, int len). Write (char[] cbuf, int off, int len).

Code usage demo:

public static void main(String[] args) throws IOException {

    FileWriter fr = new FileWriter("IO stream / / which xt");
    // The string is converted to a byte array
    char[] b = "You are so good.".toCharArray();
    // Write out the character array
    fr.write(b);  // You are good
    // Write from index 2, 2 characters. Index 2 is' stick ', two characters, 'stick'
    fr.write(b,2.2);  // You are good good goodfr.close(); } Result: You bang bang oh bangCopy the code
  • Write a string:write(String str)write(String str, int off, int len), can write out the data in the string each time, more convenient.

Code usage demo:

public static void main(String[] args) throws IOException {
    FileWriter fr = new FileWriter("IO stream / / which xt");
    / / string
    String msg = "The sister who rode the wave.";
    // Write out the character array
    fr.write(msg); // The sister who rides the wind and waves
    // Write from index 2. Index 2 is' break ', write two characters, namely 'break wave'.
    fr.write(msg,2.2); // The sister who rides the wind and waves wavesfr.close(); } Result: ride the wind and waves of sister break the wavesCopy the code
  • Continuation and newline: Operates like FileOutputStream

    • FileWriter(String fileName, boolean append)
    • FileWriter(File file, boolean append)

Parameters:

  • FileName, file: indicates the destination for writing data
  • Append: continuation switch. True: The created object does not overwrite the source file and continues to append data to the end of the source file. False: Create a new file to overwrite the source file
public static void main(String[] args) throws IOException {
    FileWriter fr = new FileWriter("IO stream / / which xt".true);
    for (int i = 0; i < 4; i++) {
        fr.write("Append");
        fr.write("\r\n"); } fr.flush(); fr.close(); } Output result: Hello bang bang oh bang bang ride the wind and waves of elder sister break wave add write add write add write add write add writeCopy the code

Newline symbol:

  • window:\r\n

  • linux:\n

  • mac:\r

  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.