Http://www.cnblogs.com/hellokitty1/p/4456305.html
Java I/O system
I:input input O:output Output
II: Classification of Streams
By direction: input stream output stream
By minimum unit: byte stream character stream (char)
Three:
All I/O system operations are composed of the following steps
1) Create a stream
2) Operation Flow
3) Close the stream
Four: File class
The file class in the Java.io package provides basic functionality for managing disk files and directories
There are four ways to construct the file class
The most common is the public File (URI uri) URI, which is the resource uniform identifier
Java.io.File Some of the methods, the main is to view the API documentation.
1 package Com.lovo; 2 3 Import Java.io.File; 4 Import java.io.IOException; 5 Import Java.util.Date; 6 7/** 8 * File class Test 9 * ten * @author hellokitty11 *12 */13 public class Filetest {in public static void main ( String[] (args) {16//Create file object: File File = new file ("E:\\jg\\exercise_bak.txt"); 18 19//can read 20 SYSTEM.OUT.PRINTLN ("can read:" + file.canread ()); 21 22//Delete System.out.println ("Delete succeeded:" + file.delete ( ); 24 25//re-create the file object in the document = new file ("E:\\jg\\exercise_bak.txt"); 27 28//Determine if the file exists in Sys Tem.out.println ("presence:" + file.exists ()); 30 31//directory or file name ("Name:" + file.getname ()) 33 34//Whether directory, file System.out.println ("Whether directory:" + file.isdirectory ()); System.out.println ("Whether file:" + F Ile.isfile ()); 37 38//Last Modified on SYSTEM.OUT.PRINTLN ("Last modified:" + New Date (File.lastmodified ())); 40 41 File Size System.out.println ("File size: "+ file.length ()); 43 44//re-create the file object. File = new file (" E:\\JG "); System.out.println (" File directory list: "); 48//Returns an array of strings that specify files and directories in the directory represented by this abstract pathname string[] list = File.list (); Tring:list) {System.out.println (string); 52}5354 55//Returns an abstract path to the an array group that represents the file in the directory represented by this abstract path name Object file[] files = File.listfiles (); (File item:files) {if (Item.isdirectory ()) { The current file object is a directory, then the directory is traversed by all subdirectories and files System.out.println (Item.getname () + "directory sub-directory and Files:"); ing[] it = Item.list (); (String i:it) {System.out.println (i); 63 }64 System.out.println ("*******************************"); continue;66 }67 System.out.println (Item.getname () + "file"); 69}70 71//re-create the file object. File = NE W File ("E:\\jg\\test\\deMo\\test.txt "); if (!file.exists ()) {//The file does not exist 74//Gets the file path of the files dir = File.getparentfi Le (); if (!dir.exists ()) {//directory does not exist, create all directories that do not exist in the path Dir.mkdirs (); 78}79 80 try {81//Create Empty file System.out.println ("file is created successfully:" + file.createnewfile ()); 83 } catch (IOException e) {e.printstacktrace (); 85}86}87 88}89}
V: Byte stream byte: used to process binary files
InputStream----"FileInputStream (Read) outputstream---" FileOutputStream (write)
Input and output stream code test for byte stream
1 package Com.lovo; 2 3 Import Java.io.File; 4 Import Java.io.FileInputStream; 5 Import java.io.FileNotFoundException; 6 Import Java.io.FileOutputStream; 7 Import java.io.IOException; 8 Import Java.io.InputStream; 9 Import java.io.outputstream;10 11/**12 * Bytes Input/output stream Test * * @author HELLOKITTY15 *16 */17 public class Iotest {18 public static void Main (string[] args) {StringBuffer buffer = new StringBuffer ();//String Buffer 21 22 /* Input stream */23 InputStream in = null;24 try {26//1. Open input stream in = new Fil Einputstream ("E:\\jg\\exercise.txt"); 28//2. Read 2930 byte[] b = new byte[1024 * 4];31 int len = In.read (b); Returns the number of bytes read to, and returns 1 for reading to the end of the stream (len! =-1) {Buffer.append (new String (b, 0, Len)); BYTE parsing to string append to buffer len = In.read (b);}36//System.out.println ("read" + len + "bytes "); the PNs System.out.println (Buffer.tostring ()); (FileNotFoundException e) {e.printstacktrace (); Atch (IOException e) {e.printstacktrace (); 43/3. Releasing resources, closing input stream 44 if (in = null) {In.close} catch (IOException e) {4 8 E.printstacktrace (); 49}50}51}52 53 */output stream */5 4 OutputStream out = null;55-try {File file = new file ("D:\\test\\demo\\test.txt "); if (!file.getparentfile (). exists ()) {//file path does not exist, create all directories that do not exist in the path, File.getparentfile (). mk Dirs (); 60}61//1. Open output Stream fileoutputstream = new (file); 63//2. Write Out.write (Buffer.tostring (). GetBytes ()); catch (FileNotFoundException e) {E.pri Ntstacktrace (); 67}catch (IOException e) {e.printstacktrace (); n} finally {70//3. Releasing the output stream resource 71 if (out! = null) {try {out.close ();//has a refreshed ioex catch ( Ception e) {e.printstacktrace (); 77}78}79}80}81}
VI: Character stream char is used to process text files Reader----inputstreamreader--->filereader Write---outputstreamwrite---"FileWrite
1 package Com.lovo; 2 3 Import java.io.FileNotFoundException; 4 Import Java.io.FileReader; 5 Import Java.io.FileWriter; 6 Import java.io.IOException; 7 Import Java.io.Reader; 8 Import Java.io.Writer; 9 10/**11 * Character input/output stream Test * * @author14 *15 */16 public class IOTest2 {+-public static void main (string[] A RGS) {StringBuffer buffer = new StringBuffer (); 20 21/* Input stream */22 Reader reader = null;23 24 try {25//1. Open stream-reader = new FileReader ("E:\\jg\\exercise.txt"); 27//2. Read 28 char[] ch = new char[128]; Buffer: int len;30 do {len = Reader.read (ch); if (len = =-1) Break;34 Buffer.append (New String (CH, 0, Len)), while (len! = 1); 3 6 System.out.println (buffer.tostring ()); PNs} catch (FileNotFoundException e) {e.printst Acktrace ();TCH (IOException e) {e.printstacktrace (); 42} finally {3. Release the resource, if ( Reader! = null) {Reader.close} catch (IOException e) {e.printstacktrace (); 48}49}50}51 52 */output stream */53 54 Writer writer = null;55-try {57//1. Open stream. Writer = new FileWriter ("D:\\test.tx T "); 59//2. Write Writer.write (buffer.tostring ()); catch (IOException e) {e.printstacktrace (); 63 } finally {64//3. Release resource, if (writer! = null) {$ try {67 68 Writer.close (); IOException} catch (e) {e.printstacktrace (); 71 }72}73}74}75}
Seven: Buffer stream
1 package com.lovo.day2; 2 3 Import Java.io.BufferedInputStream; 4 Import Java.io.BufferedOutputStream; 5 Import Java.io.File; 6 Import Java.io.FileInputStream; 7 Import java.io.FileNotFoundException; 8 Import Java.io.FileOutputStream; 9 Import java.io.IOException; Ten import Java.io.InputStream; Import Java.io.OutputStream; public class Bufferedtest {$ public static void main (string[] args) {Long start = System.curr Enttimemillis (); Copybybuffer ("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "D:\\test.exe"); Long end = System.currenttimemillis (); System.out.println ("Buffer:" + (End-start)); System.out.println ("***************"); Start = System.currenttimemillis (); Copy ("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "D:\\test2.exe"); End = System.currenttimemillis (); System.out.println ("Without buffering:" + (End-start)); 25} 26 27 private static void copy (string source, string destination) {InputStream in = null; outputstre AM out = null; File File = new file (destination); if (!file.getparentfile (). exists ()) {File.getparentfile (). Mkdirs (); 34} 35 36 try {37//open stream in = new FileInputStream (source); Ream (file); 40//Operation: Read/write byte[] b = new byte[1024]; int Len; ( -1! = (Len=in.read (b))) {Out.write (b, 0, Len); 46} 47 } catch (FileNotFoundException e) {e.printstacktrace ();) catch (IOException e) {50 E.printstacktrace (); The finally {52//release resource, if (in! = null) {# try {55} In.close (); (IOException e) {E.printstacktrace (); + +} (out! = null) {. Close (); (IOException e) {e.printstacktrace (); 65} 66 }, (Copybybuffer}), (string source, string destination) {71 Bufferedinputstream in = null; Bufferedoutputstream out = null; Bufferedinputstream try {75//open stream in = new FileInputStream (source) , 8 * 1024 * 1024); The new Bufferedoutputstream (new FileOutputStream (destination), 8 * 1024 * 1024); 78//Read/write byte[] b = new byte[1024]; int Len; Bayi ( -1! = (len = in.read (b))) {Out.write (b, 0, Len); 84} (FileNotFoundException e) {86} E.printstacktrace (); (IOException e) {e.printstacktrace (); 90/Free Resources 91 if (in = null) {In.close try {94} catch (Ioexc Eption e) {e.printstacktrace (); () ()-----98 if (out! = Nu ll) {out.close (); 101} catch (IOException e) {102 E.printstacktrace (); 103}104}105}106} 107}
Buffering is faster than no buffering.
Not finished ...
Java I/O system