The IO stream in Java is the basis for implementing input/output.
The direction of the flow is different: it is divided into input stream and output stream.
According to the different units of processing data: divided into byte stream (8 bits) and character stream (16 bit).
By function: divided into node stream and processing flow
All byte-oriented stream classes inherit from the InputStream class (input stream)
or OutputStream class (output stream), these two classes are abstract classes, and we can use its subclasses to accomplish different functions.
InputStream and OutputStream are abstract classes.
InputStream abstracts the way the application reads data
OutputStream how an abstract class application writes data
The file input stream FileInputStream inherits the Inputstream,fileinputstream specifically implemented to read data on the file.
int b = In.read () reads a byte unsigned fill to int low eight bits,-1 is EOF
In.read (byte[] buf) read data is populated into BUF
In.read (byte[] buf, int start, int size) read data is populated into BUF
FileOutputStream
Basic methods of output streams:
Out.write (int b) writes out a byte to stream B with a low eight-bit write-out
Out.write (byte[] buf) writes the buffer buf to the stream
Out.write (byte[] buf, int start, int size) writes part of the BUF to the stream
Out.flush () Cleanup buffer
Out.close ();
Note that both of them are byte streams----to read raw byte stream such as data
Case 1: Reading files using FileInputStream
File File=new file ("C:", "sql.properties");
FileInputStream fis=new fileinputstream (file);
int n=0;
Byte[] B=new byte[1024];
while ((N=fis.read (b))!=-1) {
System.out.println (NewString (b,0,n));//system.out.println (New String (b, "GBK");--Control format
}
Fis.close ();
String s= "Hello World"
S.getbytes ()--byte[]
FileOutputStream fo=new FileOutputStream (F2);
Fo.write (byte[]);
Case 3: Replication of images
Bufferedinputstream && Bufferedoutputstream provides a buffer for IO operations, typically opening files for
When writing the ring read operation, the buffer stream is added to improve the performance of IO (input and output).
There is a note in FileInputStream that this method will block, meaning that when you read a file input stream, when you read a location, if you do some other processing (for example, accept some bytes to do some processing, etc.) at this time the input stream where is where the position, Will not continue to read, and Bufferedinputstream although there is a read method, but from the name can be seen, it has a buffer, it is a non-blocking method, when you read a location, do some processing, the input stream may continue to read in bytes, This achieves the effect of buffering.
Suppose that the length of a file is 100 bytes, to read it into memory, and then assume that you read only 10 bytes at a time, then read the entire file is not read 10 times ah?
Assuming the boss lets you do 100 things, the boss says, you can only finish 10 pieces a day, do you have to wait until the tenth genius to complete the 100th thing? One day you finish 10 things before noon and you may as well do a bit more in the afternoon, then maybe 100 things are done on the 9th day.
In the same vein, Bufferedinputstream is likely to read more things than you prescribe to memory to reduce the number of accesses to the IO,
In short, you have to remember that the fewer times you access the IO, the higher the performance is because the speed of CPU and memory is much larger than the speed of your hard disk or other external device.
File F=new file ("C:/wode.txt");
Byte[] b=new byte[(int) f.length ()];
Bufferedinputstream bu=new Bufferedinputstream (new FileInputStream (f));
int i=0;
while ((I=bu.read (b))!=-1) {
System.out.println (New String (b, "GBK"));
}
Character Stream:
Character Stream (Reader Writer)
Processing of characters, one character at a time (Unicode encoding)
2) The bottom of the character is still the basic byte stream
3) basic implementation of character stream
? InputStreamReader completes a byte stream parsing to a char stream, parsed by encoding
? OutputStreamWriter provides a char stream to a byte stream, which is processed by encoding
The main use of them two FileReader FileWriter
Two common classes in the reader class:
FileReader: Reads a file in the form of a character stream, basically consistent with fileinputstream usage.
BufferedReader: Reads the stream into the buffer, then reads from the buffer, and provides the ReadLine () method to read the data from the entire line in the text file.
Two common classes in the writer class:
FileWriter: Writes a file in the form of a character stream, basically consistent with fileoutputstream usage.
BufferedWriter: Outputs the buffer to the stream.
It's the difference between me and our byte stream:
InputStream and OutputStream are mainly used to deal with numbers that can be read directly as bytes.
Reader and writer are used to process text.
Experiment 1: Write the file:
File File=new file ("C:\\Test.txt");
FileWriter fw=new
FileWriter (file);//Add parameter True here to achieve the continuation of the file
String s= "Jutou look at the moon, bow down to write code";//\r\n for line breaks below window
Fw.write (s);
Fw.close ();
Case 2: Reading a file
File File=new file ("C:\\Test.txt");
BufferedReader br=new BufferedReader (new FileReader (file));
String strline =br.readline ();
System.out.println (strline);
--but we found that only one row of data was read
File File=new file ("C:\\Test.txt");
FileReader fr=new filereader (file);
int ch=0;
while ((Ch=fr.read ())!=-1) {
System.out.print ((char) ch);
}
Java.io.File is used to represent files (directories), which means that programmers can manipulate files and directories on the hard disk in the program through the file class.
The file class is used only for information (name, size, and so on) that represents a file (directory), and can not access the contents of the document.
File F=new file (".");
System.out.println (F.getcanonicalpath ());
F.getcanonicalpath ()---Get the current path
Create a new file
File D=new file (F, "demo");
if (!d.exists ()) {
D.mkdir ();
}
Create a new File object
File Text=new file (d, "text");
if (!text.exists ()) {
Text.createnewfile ();
}
Get file
File F=new file ("C:");
System.out.println (F.getcanonicalpath ());
F.getcanonicalpath ();
File[] Files=f.listfiles ();
System.out.println (arrays.tostring (files));
The FileFilter class is the filter for the action file
File F=new file ("Src/test");
File[] Files=f.listfiles (new FileFilter () {
@Override
Public Boolean accept (File pathname) {
Return Pathname.getname (). EndsWith (". Java") &&pathname.isfile ();
}
});
for (File file:files) {
System.out.println (File.getname ());
}
The Listfiles () method will pass each file in Dir to the Accept () method for detection and, if true, as the returned result element of the method
IO stream in Java