Javaio series - common character encoding, character stream abstract class, FileReader, filewriter

Concise programming 2022-06-24 07:34:46 阅读数:561

javaioseriescommoncharacterencoding

Character encoding

  1. ISO-8859-1: Collection and division ASCII Outside , And Western Europe 、 Greek 、 Thai, 、 The Arabic language 、 The Hebrew equivalent .
  2. UTF-8: in the light of Unicode Variable length character coding of code table
  3. GB2312: Simplified Chinese
  4. GBK: Simplified Chinese 、 expand
  5. BIG5: Traditional Chinese

linux System :utf-8
windows:gbk
mac:utf-8

Be careful

When the decoding and encoding methods are different, there will be garbled code !
We recommend popularizing utf-8 code

Character stream abstract class

  1. Reader: Character input stream
  2. Writer: Character output stream

Reader

An abstract class for reading character streams . The only way Subclasses must implement read(char[], int, int) and close(). most however , Subclasses will override some of the methods defined here in order Provide higher efficiency 、 Additional functions or both .

Construction method

 Insert picture description here

Method

 Insert picture description here

Writer

Abstract classes for writing character streams . The only way Subclasses must implement write(char[], int, int)、flush() and close(). However , Most subclasses will override some of the methods defined here To provide more efficiency 、 Additional functions or both .

Construction method

 Insert picture description here

Method

 Insert picture description here

FileReader

Read text from a character file using the default buffer size . Decode from bytes to characters Use specified Character set Or platform Default character set .

this FileReader Used to read a character stream . For reading Raw byte stream , Consider using FileInputStream.

structure

 Insert picture description here

Method

 Insert picture description here

Example

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class demo9 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\ New text document .txt";
FileReader fileReader = new FileReader(path);
int count = 0;
char[] strings = new char[1024];
while ((count = fileReader.read(strings))!=-1){

System.out.println(new String(strings,0,count));
}
fileReader.close();
}
}

FileWriter

Use the default buffer size to write text to a character file . From character encoding to bytes Use specified Character set Or platform Default character set .

Whether a file is available or can be created depends on Bottom platform . Especially on some platforms , Allow files Only one person is open to writing FileWriter( Or other files object ) once . under these circumstances , Constructors in this class If the file involved has been opened , It will fail .

this FileWriter For writing character streams . Writing function Raw byte stream , Consider using FileOutputStream.

structure

 Insert picture description here

Method

 Insert picture description here

Example

import java.io.FileWriter;
import java.io.IOException;
public class demo10 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
FileWriter fileWriter = new FileWriter(path);
fileWriter.write(" Anhui Normal University subsea tunnel ");
fileWriter.flush();
fileWriter.close();
}
}
copyright:author[Concise programming],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240216122126.html