Detailed knowledge IO streams

######## - IO stream of knowledge - ###################
1.file file object []
2. Using a recursive descendant class print directories and files name.
3. Statistical folder size.
4. Four abstract class: InputStream, OutputStream [byte stream, a character stream [] Writer Reader]
5. InputStreanm "reads a byte" and "a section of the read."
6. Use the OutputStream "string to the file"
7. copy byte file. [Recursive make a copy of a copy of the folder]
8. Use Writer, Reader, the "reading character files", "writing character files", "copy character file."
9. ByteArrayInputStrean] [byte array input stream, ByteArrayOutputStrean byte [] array output stream
10. The integrated stream docking [FileInputStream- "ByteArrayOutputStream-" ByteArrayInputStream- "FileOutputStream]
11.IO tools.
12. The process flow [decorative design pattern: an abstract component: Interface 2. Specific components: 3. The object to be decorated decoration abstract: contains the target component and a reference decoration common method. 4. DETAILED decoration: the object to be decorated. ]
13. [commutations byte character stream flow, can specify the character set]
14. [easy operation Eight data stream types and data String]
15. The target stream
16. The stream [print] PritStream
17. RandomAccessFile] [Random Access inflow
18.CommonsIO

######## - IO stream details - ########################
1.file file object []
① File.separator [ File.separator: -> \ String path = "d: /java/image/io.png"; ]
② new File ( "d: /java/image/io.png "); | new File ( "d: / the Java / Image / ", io.png); | new new File (new new File (" d: /java/image/io.png "));
③ getAbsolutePath (); // get the absolute path [System.getProperty (" user .dir ") the user's current path]
③ getName () [filename] getPath () [path] get getParent () get the parent [path]
④ exists () [(file or folder) the existence of] isFile () {if file] isDirectory () [whether] folder
⑤ createNewFile () [does not exist to create a new file, there is no creation] delete () [delete file]
⑥ length () [file size]
⑦mkdir () to create a file [ folder, the parent directory must exist] mkdirs () [] directly create
list () [name] lists the subordinate listFiles () [list] subordinate File object listRoots () [lists all the letters: d: \ c: \]
⑧ encoding: The character - "byte
String msg = "mission life Life"; byte [] datas = msg.getBytes (); // utf-8 3 bytes Chinese
decoding: byte - "character
msg = new String (datas, 0 , datas.length, "UTF8");
2. four abstract classes: InputStream, OutputStream [[byte stream, a character stream Writer Reader]]
3.ByteArrayInputStrean] [byte array input stream, ByteArrayOutputStrean ByteArrayOutputStream] [
① byte array stream not need to close to close by the JVM. Anything can turn a byte array. Byte arrays can not be too large.
A ByteArrayInputStream in the InputStream = new new ② (byte []);
the OutputStream new new ByteArrayOutputStream OUT = (); | the toByteArray (): Get data
4. Processing flow] [decorative stream
①BufferedInputStream: caching the input stream of bytes. [Only need to release BufferedInputStream, it will automatically release the other flow-related]
②BufferedOutputStream: byte caching the output stream. [Only need to release BufferedOutStream, it will automatically release the other flow-related]
③BufferedReader: byte buffer input stream.
[(1) only need to release BufferedReader, flow will automatically release of other relevant (2) readLine ();}
④BufferedWriter: buffer byte output stream.
[(1) BufferedWriter, just released, is automatically released other related flow (2) newLine ();}
5. {commutations effect: (1) transfer byte character stream, (2) may specify the character set]
①InputStreamReader:
②InputStreanWriter:
③ acquiring network input stream of bytes: the InputStream in = new new ( "http://www.baidu.com") .OpenStream () the URL;
6. The data stream [ease of operation and eight data type String}
DataInputStream | the DataOutputStream
. 7 The object stream
ObjectInputStream: serialization | persistence (Interface: the serialization)
the ObjectOutputStream: deserialization
Usage transient keyword may not be serialized]
8. The print stream [PrintStream, PrintWriter]
PritStream the System.out = PS;
PS = new new PritStream (new new BufferedOutputStream (new new FileOutputStrean ( "print.txt")), to true);
ps.println ( "print streams"); // You can print the contents of a file to print.txt.
/ Redirect the output terminal /
System.setOut (PS);
System.out.println(“jjj”)

PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStrean(“print.txt”)),true);
pw.println(“打印流”);

  1. [RandomAccessFile random access inflow]
    ① specified start position, reading all remaining contents.
    ② document division
    ③ document merger: [sequence flow: SeauenceInputStream]
    the Vector the Vector new new VI = ();
    A SequenceInputStream new new = A SequenceInputStream SIS (vi.elements ());
    10.CommonsIO
    ① Get File Size: FileUtils.sizeOf (File f); // f specified file
    ② get directory size: FileUtils.sizeOf (file f) // f specified directory
    ③ lists descendants grade: Collection file = FileUtils.listFiles (new file ( "D: \ java300 \ IO_study04"), // select node
    FileFilterUtils.and (new SuffixFileFilter ( "java" ), EmptyFileFilter.NOT_EMPTY), // filter conditions
    DirectoryFileFilter.INSTANCE); //
    ④ read the file: String msg = FileUtils.readFileToString (new file ( "emp.txt" ), "UTF-. 8");
    byte [] = FileUtils.readFileToByteArray DATAS (new new File ( "emp.txt"));
    Msgs = FileUtils.readLines List (new new File ( "emp.txt"), "UTF-8");
    ⑤ write file: FileUtils.write (new ( "happy.sxt" File), " learning is a great cause \ r \ the n-"," UTF-8 ");
    FileUtils.writeStringToFile (new new (File" happy.sxt ")," learning is a hard business \ r \ n "," UTF -8 ", true);
    FileUtils.writeByteArrayToFile (new ( "happy.sxt" File ), " learning is a happy career \ r \ the n-" .getBytes ( "UTF-8"), to true);
    List DATAS = new new ArrayList (); FileUtils. writeLines (new file ( "happy.sxt" ), datas, " link character", to true);
    ⑥ copy the file: FileUtils.copyFile (new file ( "p.png "), new file ( "p-copy.png" )); // copy the file
    FileUtils.copyFileToDirectory (new file ( "p.png" ), new file ( "lib")); // copy the file to the directory
    FileUtils.copyDirectoryToDirectory (new file ( "lib" ), new file ( "lib2")); // copy the directory to the directory
    FileUtils.copyDirectory (new File ( "lib" ), new File ( "lib2")); // copy directory
    // copy the URL content; FileUtils.copyURLToFile (new ( "https://pic2.zhimg.com/v2-7d01cab20858648cbf62333a7988e6d0_qhd.jpg"), new File ( "marvel.jpg") URL)
Published 20 original articles · won praise 0 · Views 1210

Guess you like

Origin blog.csdn.net/jinhuding/article/details/104837071