A, common streams
1, byte stream
Overview: everything is byte, byte, the smallest computer storage unit. Pictures, videos stored in binary, one by one byte, the byte stream can read any file.
1 "output stream of bytes
superclass ---- a java.io.OutputStream all bytes output" sub-class FileOutputStream file output stream of bytes
configured:
FileOutputStream (File name);
FileOutputStream (String path);
Method:
Close ( ); flow off
the flush (); refresh
write (byte [] byet); writing
steps:
1. Create FileOutputStream object constructor bound to write data source (file address)
2, using an object .write () ; write file
3, the release of resources, off flow (first opened after closing, after opening the first turn)
2 "byte input stream
java.io.InputStream all input stream of bytes superclass ----" subclass FileInputStream file input stream of bytes
configuration:
the FileInputStream (file name);
FileInputStream (String path);
Method:
Read (); Reads the next byte
read (byte [] bytes); a plurality of read bytes, the byte array parameter cushion, the length of 1KB = 1024B
Close ( ); off stream
steps:
1. Create FileInputStream object constructor bound to read data source (file address)
2, using an object .read (); read file
3, the release of resources, flows off (after the first opening relations, after the opening of the first turn)
2, the character stream
1 "character input stream
java.io.Reader top parent character input stream ----" the InputStreamReader subclass ---- "subclasses FileReader character input stream file
structure:
FileReader (File name);
FileReader (String path);
method:
Read (int len);
Read (char [] chars);
Read (String STR, OFF int, int len);
Close ();
step:
1, to create FileReader object constructor bound to read the data source
2, using the object .read (); read data
3, to release resources
2 "character output stream
superclass ---- a java.io.Writer character output stream" subclass the OutputStreamWriter ---- "subclass FileWriter character file output stream
configured:
FileWriter (file name);
FileWriter (String path);
method:
Write (int len);
Write (char [] chars);
Write (String STR, OFF int, int len);
the flush ();
Close ();
Step:
1, to create FileWriter object constructor bound to read data source
2, using the object .write (); ---- data written into the buffer
3, the object .flush (); ---- the buffer data written to a file
4, the release of resources
★, flush with close distinguish
flush (); refresh the stream object, forced to clean up the buffer, none of flow can continue to use the
close (); flush the buffer, then way off by releasing resources flow, the flow can no longer use
writing, line:
FileWriter (String fileName , boolean append); additional writing true, false unsuccessful
line breaks: Windows \ r \ the n-
Linux / the n-
mac / r
Second, the flow exception handling
before the JDK7, using try, catch, finally processing stream exception
try {
possible exception code;
} the catch (Exception name) {
exception handling operation;
} {the finally
code that are executed;
}
JDK9 new features:
1 , may be defined in front try stream object
2, after the try () can be directly introduced into the name (variable name) stream object
3, after the try is finished execution, the stream object can be released directly, without writing finally
Three, Properties (Key)
Overview: java.util.Properties collection, ---- "extends HashTable <k, v> implements Map <k, v> represents a persistent set of properties, Properties may be stored in the stream, or from loaded stream, and is the only one
IO flowing bound set.
Method:
Store (); the temporary data stream, to the hard disk storage persistence
Load (); hard disk file (key pair) using the read set
★ Properties collection used to store data, taken to traverse
the setProperties ( String key, String value); setting key
getProperties (String key); key value acquired values based
stringPropertiesNames (); ---- "corresponds keySet method, a save key to set the new
Fourth, the buffer flow
Overview: BufferedStream to improve the efficiency of reading and writing.
1, byte buffered output stream
BufferedOutputStream ---- "extends OutputStream
configured:
new new BufferedOutputStream The (the OutputStream OS);
Method:
Write ();
the flush ();
Close ();
Step:
1, to create FileOutputStream object, binding file object to
2, BufferedOutputStream create an object, the object wrapped FOS
3, using the object BufferedOutputStream .write (); written to the buffer
4, the use of the object BufferedOutputStream .flush (); to buffer write data in the file
5, the release of resources
2 bytes buffered input stream
BufferedInputStream ---- "extends InputStream
configured:
new new BufferedInputStream (the InputStream IS);
method:
Read ();
Close ();
Step:
1, to create an object FileInputStream, binding file destination
2, BufferedInputStream create objects, objects wrapped FIS
3, using the object BufferedInputStream .read (); read data
4, to release resources
Such as:
Package cn.kgc.demo01;
Import the java.io. *;
/ **
* the Created by ZhaoQiannan ON 2019/5/26 17:18
* /
public class CopyTest {
// main method
public static void main (String [] args) {
the try {
IO ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
// buffer write stream
Private static void IO () throws IOException {
// start time
long start = System.currentTimeMillis ( );
FileInputStream fis=null;
BufferedInputStream bis=null;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try {
// FileInputStream对象
fis = new FileInputStream("e:\\Test\\mm.jpg");
// BufferedInputStream对象
bis=new BufferedInputStream(fis);
// FileOutputStream对象
fos=new FileOutputStream("e:\\Test\\mv.jpg");
// BufferedOutputStream对象
bos=new BufferedOutputStream(fos);
// 定义数组,读写数据
byte[] b=new byte[1024];
int len=0;
while ((len=bis.read(b))!=-1){
bos.write(b,0,len);
bos.flush();
}
} The catch (a FileNotFoundException E) {
e.printStackTrace ();
} the finally {
// release
the try {
bos.close ();
fos.close ();
bis.close ();
fis.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
// end time
Long end = System.currentTimeMillis ();
// time used
Long seconds the end = - Start;
System.out.println ( "replication with time" + seconds);
}
}