Concise programming 2022-06-24 07:34:46 阅读数:561
linux System :utf-8
windows:gbk
mac:utf-8
When the decoding and encoding methods are different, there will be garbled code !
We recommend popularizing utf-8 code
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 .
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 .
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.
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();
}
}
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.
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