Javaio series - BufferedReader, bufferedwriter, printwriter, transform stream

Concise programming 2022-06-24 07:34:52 阅读数:47

javaioseriesbufferedreaderbufferedwriterprintwriter

BufferedReader,

Read text from character input stream , Buffer characters so that Provide characters 、 Effective reading of arrays and rows .

You can specify the buffer size , You can also use the default size . this The default value is large enough for most purposes

structure

 Insert picture description here

Method

In particular, we have more readLine Method can read a whole line
 Insert picture description here

Example

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class demo11 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\ New text document .txt";
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
while ((line= reader.readLine())!=null){

System.out.println(line);
}
reader.close();
}
}

BufferedWriter

Write text to character output stream , Buffer characters so that Provide a single character 、 Efficient writing of arrays and strings .

You can specify the buffer size , You can also accept the default size . For most purposes , The default value is large enough .

Provides a newLine() Method , It uses the platform's own concept Line delimiters defined by system properties line.separator. Not all platforms use line breaks (‘\n’) To terminate the row . therefore , It is best to call this method to terminate each output line Write a line break directly .

structure

 Insert picture description here

Method

 Insert picture description here

Example

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class demo12 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
bufferedWriter.write(" Satan card or something adsadhsahdkk");
bufferedWriter.newLine();
bufferedWriter.write("565446464");
bufferedWriter.flush();
bufferedWriter.close();
}
}

PrintWriter

Encapsulates the print() and println() Method

Print the formatted representation of the object to the text output stream . This Class implements all print Methods found in PrintStream. It does not contain methods for writing raw bytes , among The program should use an uncoded byte stream .

Unlike PrintStream class , If auto refresh is enabled Only if one of them println, printf, perhaps format Method is called , Instead of every newline It happens to be the output . These methods use the platform's own line concept Separator instead of newline .

Methods in this class never throw I/O abnormal , Although some of it Constructors may . The customer can ask if there are any errors Occurs by calling checkError().

This class always replaces malformed and unmapped character sequences with The default replacement string for the character set . CharsetEncoder Use Classes should be used more often The coding process needs to be controlled

structure

 Insert picture description here

Method

 Insert picture description here
 Insert picture description here
 Insert picture description here

Example

import entity.Stu;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class demo13 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
Stu stu = new Stu("zhansgan",55,102.31);
PrintWriter printWriter = new PrintWriter(path);
printWriter.println(209.31);
printWriter.println("helloword");
printWriter.write("new");
printWriter.print(2012);
printWriter.close();
}
}

Converted flow ( a key )

You can convert a byte stream to a character stream !!!
You can set the character encoding method !!!

InputStreamReader

InputStreamReader It's a bridge from byte stream to character stream : it Reads bytes and decodes them into characters using the specified characters charset. The character set it uses You can specify by name , It can also be given explicitly , Or platform You can accept the default character set .

Every time you call InputStreamReader Of read() One of the methods may be Causes one or more bytes to be read from the underlying byte input stream . In order to effectively convert bytes to characters , More bytes can be used Read ahead from the underlying stream , Instead of being satisfied Current read operation .

For maximum efficiency , Please consider that InputStreamReader It's packed in Buffer reader

structure

 Insert picture description here

Method

 Insert picture description here

Example

import java.io.*;
public class demo14 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path), "UTF-8");
int count = 0;
while ((count= inputStreamReader.read())!=-1){

System.out.print((char) count);
}
inputStreamReader.close();
}
}

OutputStreamWriter

OutputStreamWriter It's a bridge from character stream to byte stream : The characters written to it are encoded as charset. The character set it uses You can specify by name , It can also be given explicitly , Or platform You can accept the default character set .

Every time you call write() Methods will cause the codec Call... On a given character . The result bytes accumulate in a Buffer before writing to the underlying output stream . Please note that , Pass to write() Method's characters are not buffered .

For maximum efficiency , Please consider that OutputStreamWriter It's packed in BufferedWriter To avoid frequent converter calls .

structure

 Insert picture description here

Method

 Insert picture description here

Example

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class demo15 {

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

String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
outputStreamWriter.write("hello world !!! Hello world ");
outputStreamWriter.flush();
outputStreamWriter.close();
}
}
copyright:author[Concise programming],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240216122055.html