Summary of Node Stream and Character Stream of Java IO Stream
- 2021-09-24 22:33:00
- OfStack
1. File
file is an abstract representation of file and directory pathnames
1.1 Usage of File
Usage:
File file = new File(" Path name ");
// Such as
File file = new File("L:\\FileTestDemo\\AAA\\aaa.txt");
Note: In windows, pathnames cannot use a single
\
, individual
\
To escape characters, you can use the
\\
,
//
Or
/
1.2 Common Methods of File
1.boolean createNewFile()
Create a new empty file named by this abstract pathname atomically if and only if a file with this name does not already exist. Note: You cannot create folders, only files
System.out.println(file.createNewFile()); //true or false Indicates whether the creation was successful
2.boolean delete()
Delete the file or directory represented by this abstract pathname.
System.out.println(file.delete());
3.boolean exists()
Test whether the file or directory represented by this abstract pathname exists.
if(!file.exists()){
System.out.println(" File does not exist, create "+ file.createNewFile());
}
4.String getName()
Returns the name of the file or directory represented by this abstract pathname.
System.out.println(file.getName());
5.long lastModified()
Returns the time when the file represented by this abstract pathname was last modified.
System.out.println(new SimpleDateFormat().format(new Date(file.lastModified())));
2. IO Stream
Class File:
You can only operate the external contents of the file, but you cannot read and write the internal contents of the file
Stream:
Data is transmitted in a first-in, first-out order, and a stream is a pipeline, which is used to transmit data
IO Stream:
java provides an io package, provides many classes, and provides many functions to realize data reading and writing
2.1 The Concept of Flow
Data source is like water tank, flow is like water flow in water pipe, and program is our final user. Stream is an abstract and dynamic concept, and it is a series of continuous and dynamic data sets
2.2 Classification of Streams
According to the flow direction, it can be divided into input stream (InputStream and Reader) and output stream (OutputStream and Writer)
It can be divided into byte stream (InputStream and OutputStream) and character stream (Reader and Writer) by operation unit
By function, it can be divided into node flow (which can read and write data directly from data source or destination) and function flow (which is the flow of processing flow). Improve the performance of the program by processing other streams)
2.3 IO Operating Steps
Before doing anything, you must first clarify the purpose (read or write), find the source (read) and find the destination (write)
1. Connect: This step is to get the stream. If this is a file, you need to abstract the file into memory to form an object. It can also be other data sources in the later period
2. Choose flow: Consider reading and writing, data unit and function. Input and output, byte character, node stream processing stream.
3. Perform actions: Read and read, write and write. Consider that it is completed once, and it needs to be cycled.
4. Release resources: The file IO resources opened in the program do not belong to the resources in memory, and garbage collection cannot be recovered, so it needs to be displayed and closed.
2.4 Basic Input Streams (InputStream and Reader)
int read() - > The return value is what you read this time (1 byte value, 1 character value)
int read(byte[] car) - > Temporarily stores the read bytes in the car array, returning the actual number of bytes read
int reade(char[] car) - > Temporarily stores the read characters in the car array, returning the actual number of characters read
Whatever way read (), read ([]) reads the file,-1 means reading to the end of the file
Character- > You can only manipulate text, and what you read directly is characters
Byte- > Arbitrary data, but if the operation is character, we need to convert it (trouble)
Single byte read
Take InputStream as an example, and Reader is the same
public class IOTest {
public static void main(String[] args) throws IOException {
// Select a stream and establish a connection
InputStream is = new FileInputStream("L:\\FileTestDemo\\123.txt");
// In any way read(), read([]) Read the file, -1, It means reading to the end of the file
// Then when result The value of is -1 End of hour
int result = -1;
while((result = is.read()) != -1){
System.out.println((char)result);
}
// Shut down
is.close();
}
}
Multiple byte read
public class IOTest {
public static void main(String[] args) throws IOException {
// Select a stream and establish a connection
InputStream is = new FileInputStream("L:\\FileTestDemo\\123.txt");
// Defining a shipping array
byte[] car = new byte[1024];
int len = -1;
// When result The value of is -1 End of hour
while ((len = is.read(car)) != -1){
System.out.println(new String(car,0,len));
}
// Shut down
is.close();
}
}
2.5 Basic Output Streams (OutputStream and Writer)
Take OutputStream as an example, and Writer is the same
Note: You need to empty the (flush) buffer before closing the (close) output stream
flush () 1 is mainly used in IO, that is, empty the buffer data, that is to say, when you use the read-write stream, the data is actually read into the memory first, and then written to the file with the data. When you finish reading the data, it does not mean that your data has been written, because there is still a part that may remain in the buffer of memory. If you call the close () method to close the read-write stream, this data will be lost, so you should clear the data before closing the read-write stream with flush ().
Single byte write
public class OutputStream01 {
public static void main(String[] args) throws IOException {
Select a stream and establish a connection
OutputStream os = new FileOutputStream("L:\\FileTestDemo\\abc.txt",true);
// Write data
os.write('a');
// Brush outflow
os.flush();
// Shut down
os.close();
}
}
Multibyte input
public class OutputStream02 {
public static void main(String[] args) throws IOException {
// Build the output stream
OutputStream os = new FileOutputStream("L:\\FileTestDemo\\abc.txt");
// Data to be written
String str = " I'm so lame \n" +
" This is it? \n" +
"\t\t\t Oh, no, ah sir";
// Convert byte data into array
byte[] arr = str.getBytes();
// Write data
os.write(arr);
// Brush out data
os.flush();
// Shut down
os.close();
}
}
2.6 Combined use of input and output
Take InputStream and OutputStream as examples
System.out.println(file.createNewFile()); //true or false Indicates whether the creation was successful
0
2.7 Exception Handling
Compile-time exceptions occur when using IO streams, which can be handled by throwing exceptions or crawling exceptions
Throw an exception
System.out.println(file.createNewFile()); //true or false Indicates whether the creation was successful
1
Grasp anomaly
System.out.println(file.createNewFile()); //true or false Indicates whether the creation was successful
2
The difference between 2.8 byte stream and character stream
The difference between byte stream and character stream:
1. The number of bytes read and written each time is different;
2. Character stream is block read and write, byte stream is byte read and write;
3. Character stream has cache, byte stream does not.
java streams are divided into character streams and byte streams in processing. The character stream processing unit is a 2-byte Unicode character that operates on characters, character arrays, or strings, while the byte stream processing unit is a 1-byte that operates on bytes and byte arrays.
Character stream and byte stream, one attribute range is small and one attribute range is large. Character stream can only be one type of characters, but byte stream can be characters, binary files, audio and various types. As long as it conforms to byte storage, byte stream can be connected, while character stream can only be connected with characters.