Java SE Chapter 8 Streams and Files-Byte Stream, Character Stream

1. Byte stream
InputStream is the parent class of all byte input streams.
Method list:
abstract int read()
int read(byte[] b)
int read(byte[] b,int offset,int len)
int available()
void close()
This class is an abstract class, and you want to read data You must use its subclass to read data by calling the read method by creating a stream object.
Its common subclasses:
FileInputStream
ByteArrayInputStream
FilterInputStream
PipedInputStream
ObjectInputStream
Example: Use FileInputStream to read and print the contents of the test.txt file in the D drive to the console.

FileInputStream fi=null;
fi=new FileInputStream("D:\\test.txt");
int value=fi.read();
System.out.println((char)value);

OutputStream is the parent class of all byte output streams.
Method list:
void write(int c)
void write(byte[] b)
void write(byte[] b, int offset, int len)
void close()
void flush()
This class is also an abstract class, want to data To read, you must use its subclass, and call the read method to read data by creating a stream object.
Common subclasses:
FileOutputStream
ByteArrayOutputStream
FilterOutputStream
PipedOutputStream
ObjectOutputStream
Example: Use FileOutputStream to write the content (10 A) into the file D:\test.txt.

FileOutputStream fo=null;
fo=new FileOutputStream("D:\\test.txt");
for(int i=0;i<10;i++){
    
    
fo.write(65);
}

Filter flow: Realize the connection and encapsulation of an existing flow.
FilterInputStream is the filtering input stream.
Common subclasses:
DataInputStream
BufferedInputStream
LineNumberInputStream
PushbackInputStream
Example: Use BufferedInputStream to read (10 A) from the test.txt file in Disk D.

BufferedInputStream bi=null;
bi= new BufferedInputStream(new FileInputStream("D:\\test.txt"));
int result=0;
while((result=bi.read())!=-1){
    
    
System.out.print((char)result);
}

FilterOutputStream is the filtered output stream.
Common subclasses:
DataOutputStream
BufferedOutputStream
PrintStream
Example: Use BufferedOutputStream to write data to the test.txt file in Disk D.

BufferedOutputStream bo=null;
bo=new BufferedOutputStream(new FileOutputStream("D:\\test.txt"));
for(int i=0;i<10;i++){
    
    
bo.write(65);
}

2. Character stream
Reader class is the parent class of all classes of character input stream.
Method list:
int read()
int read(char[]buffer)
int read(char[]buffer,int offset int len
)
common subclasses of void close() :
CharArrayReader
BufferedReader
StringReader
FileReader
InputStreamReader
Example: Use FileReader and BufferedReader to read D The contents of the test.txt file in the root directory of the disk.

BufferedReader br=null;
br=new BufferedReader (new FileReader("D:\\test.txt"));
String result=null;
while((result=br.readline())!=null){
    
    
System.out.println(result);
}

The Writer class is the parent class of all classes of character output streams.
Method list:
void write(int c)
void write(char[]buffer)
void write(char[]buffer,int offset,int len
)
common subclasses of void write(String str) :
CharArrayWriter
BufferedWriter
StringWriter
FileWriter
OutputStreamWriter
Example: Using FileWriter And BufferedWriter write ("hello world") into D:\test.txt.

BufferedReader bw=null;
bw=new BufferedReader (new FileWriter(D:\\test.txt));
String line=System.getProperty("line.separator");
bw.write("hello world");
bw.flush();

Guess you like

Origin blog.csdn.net/qq_45618376/article/details/111397492