2019-05-31 Java Learning Diary day21 IO Stream Character Stream
Posted by gausie on Sat, 01 Jun 2019 01:55:50 +0200
IO
Character stream
Character streams are IO streams that can read and write characters directly
When character stream reads characters, it reads byte data first and then converts them to characters. If character is written, it needs to be converted to bytes before writing out.
FileReader
The read () method of the FileReader class can be read by character size
public class demo1_filereader {
public static void main(String[] args) throws IOException {
//demo1();
FileReader f1 =new FileReader("xxx.txt");
int x;
while((x =f1.read()) != -1){ //Read a character at a time through the project's default code table
System.out.print((char)x);
}
}
public static void demo1() throws FileNotFoundException, IOException {
FileReader f1 =new FileReader("xxx.txt");
int x=f1.read();
System.out.println(x);
char c=(char)x;
System.out.println(c);
f1.close();
}
}
case
The write () method of the FileWriter class automatically writes characters into bytes
public static void main(String[] args) throws IOException {
FileWriter f1=new FileWriter("tan.txt");
f1.write("Hello everyone");
f1.close();
}
}
case
Copy of character stream
public class demo3_fierreader {
public static void main(String[] args) throws IOException {
FileReader f1 =new FileReader("xxx.txt");
FileWriter f2 =new FileWriter("zzz.txt");
int c;
while((c =f1.read()) != -1){
f2.write(c);
}
f1.close();
f2.close(); //writer There is a 2 in the class k A small buffer, if it does not shut down, will write content to the buffer, shutdown will, the buffer will refresh the buffer
}
}
case
When to Use Character Stream
* Character streams can also copy text files, but they are not recommended because bytes are converted to characters when read and bytes when written.
* A program needs to read a paragraph of text, or it can use character streams when it needs to write a paragraph of text.
When reading according to the size of the characters, there will be no half Chinese.
When writing, you can write strings directly without converting them to byte arrays
Can Character Stream Copy Files with Non-plain Text
It is not possible to copy non-plain text.
Because bytes are converted to characters when they are read, when the corresponding characters may not be found in the conversion process, they will be used? Instead, when written, the string is converted into bytes and written out.
If so? Write it out directly, and then the documents will be confused and can't be seen.
Copies of custom character arrays
FileReader f1 =new FileReader("xxx.txt");
FileWriter f2 =new FileWriter("yyy.txt");
char [] arr=new char[1024];
int len;
while((len =f1.read(arr)) != -1){ //Read data from a file into an array of characters
f2.write(arr,0,len); //Write data from character arrays to files
}
f1.close();
f2.close();
Buffered character stream
BufferedReader's read () method reads characters at one time and then returns them to the program one by one, which reduces the number of files read and improves efficiency.
BufferdWriter's write () method writes a buffer first when it writes a character, and when the buffer is full, it writes a file, which reduces the number of writes and improves efficiency.
readLine () and newLine () methods
BufferedReader's readLine () method reads a line of characters (excluding newline symbols)
public class demo4_buffered {
public static void main(String[] args) throws IOException {
BufferedReader b1 =new BufferedReader(new FileReader("xxx.txt"));
String line;
while((line = b1.readLine()) != null){
System.out.println(line);
}
b1.close();
}
}
case
BufferdWriter's newLine () can output a cross-platform newline symbol " r n"
newLine () is a cross-platform approach
"rn" only supports windows systems
BufferedReader b1 =new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter b2=new BufferedWriter(new FileWriter("yyy.txt"));
String line;
while((line= b1.readLine()) !=null){
b2.write(line);
b2.newLine();
}
b1.close();
b2.close();
case
Inversion of text
public class test1 {
public static void main(String[] args) throws IOException {
//Creating input and output stream objects
BufferedReader br =new BufferedReader(new FileReader("xxx.txt"));
//Creating Collection Objects
ArrayList<String> list =new ArrayList<>();
//Store read data in a collection
String line;
while((line =br.readLine()) !=null){
list.add(line);
}
br.close(); //Shut off flow
//Traveling backwards to collect the documents written by Secretary Jiang
BufferedWriter bw =new BufferedWriter(new FileWriter("jung.txt"));
for (int i = list.size() -1; i >=0; i--) {
bw.write(list.get(i));
bw.newLine();
}
bw.close();
}
}
Late opening and early closing
LineNumberReader
LineNumberReader, a subclass of BufferedReader, has the same functionality and can count line numbers
* Call the getLineNumder () method to get the current line number
* Calling the getLineNumder () method sets the current line number
public class demo5_number {
public static void main (String args []) throws IOException{
LineNumberReader num1=new LineNumberReader(new FileReader("jung.txt"));
String line ;
num1.setLineNumber(100);
while((line =num1.readLine()) !=null){
System.out.println(num1.getLineNumber()+":"+line);
}
num1.close();
}
}
case
Topics:
PHP
Windows