Detail the use of buffer flow for java IO flow
- 2020-05-30 20:01:45
- OfStack
The java buffer flow itself does not have the IO function, but simply adds a buffer on top of another stream to increase efficiency, as if it were a wrapper for another stream. Poor performance when reading and writing to files or other targets frequently or when operating inefficiently. Using a buffer flow makes it more efficient to read and write information. Because the buffer stream first caches the data and then writes or reads it out. Therefore, it is important to remember to add buffer flow to IO operation to improve performance.
Buffer streams are divided into byte and character buffer streams
The byte buffer flow is:
BufferedInputStream - byte input buffer stream
BufferedOutputStream - byte output buffer stream
The character buffer flow is:
BufferedReader - character input buffer flow
BufferedWriter - character output buffer flow
The use of these four types of buffer flows is described below.
1. Byte buffer flow
1.BufferedOutputStream - byte output buffer stream
The BufferedOutputStream class implements buffered output, and by setting up this output stream, the application can write individual bytes to the underlying output stream instead of calling the underlying system for each byte write.
Sample code:
public static void main(String[] args) {
try {
// Create an instance of the byte output stream
OutputStream out=new FileOutputStream("L:\\test.txt");
// Build a byte buffer stream based on the byte output stream
BufferedOutputStream buf=new BufferedOutputStream(out);
String data=" study hard , Day day up ";
buf.write(data.getBytes());// Write buffer
buf.flush();// Flush the buffer, that is, write the content
// Close the stream
buf.close();// When the buffer flow is turned off , Will refresh 1 A buffer
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.BufferedInputStream - byte input buffer stream
BufferedInputStream adds buffering to other input streams. When creating BufferedInputStream, an internal buffering array is created to buffer data and improve performance.
Sample code:
public static void main(String[] args) {
try {
// Create an instance of the byte input stream
InputStream in=new FileInputStream("L:\\test.txt");
// Build a byte buffer stream based on the byte input stream
BufferedInputStream buf=new BufferedInputStream(in);
byte[]bytes=new byte[1024];
// Data is read
int len=-1;
StringBuffer sb=new StringBuffer();
while((len=buf.read(bytes))!=-1)
{
sb.append(new String(bytes,0,len));
}
System.out.println(" Content as follows: "+sb);
// Close the stream
buf.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2. Character buffer flow
1.BufferedWriter -- character output buffer flow
Writing text to the character output stream, buffering individual characters, provides efficient writing. You can specify the size of the buffer; in general, the default buffer size is sufficient.
Sample code:
public static void main(String[] args) {
try {
Writer w=new FileWriter("L:\\test.txt");
// Create a character buffer flow based on the character output stream
BufferedWriter buf=new BufferedWriter(w);
// Write data
buf.write(" It takes time to grind a deep iron pestle into a needle ");
// Refresh the flow
buf.flush();
// Close the stream
buf.close();
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.BufferedReader -- character input buffer flow
Read information from the character input stream and buffer individual characters to achieve efficient reading. You can specify the size of the buffer; in general, the default buffer size is sufficient. The default size is 8192.
Sample code:
public static void main(String[] args) {
try {
Reader r=new FileReader("L:\\test.txt");
// Create a character buffer flow based on the character input stream
BufferedReader buf=new BufferedReader(r);
char [] data=new char[512];
// Data is read
int len=-1;
StringBuilder sb=new StringBuilder();
while((len=buf.read(data))!=-1)
{
sb.append(new String(data,0,len));
}
System.out.println(" Content is: "+sb);
// Close the stream
buf.close();
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}