The main thing about Java IO is the input and output of the file, so I study the system
There are several key classes of the main File fileinputstream fileoutputstream inputstreamreader outputstreamwriter bufferedreader BufferedWri ter Bufferedinputstream Bufferedoutputstream
Below I sit down the detailed introduction:
The file class is the most basic class of files
You can get the basic properties of a file.
Import Java.io.file;import Java.io.ioexception;public class Test01 {public static void main (string[] args) throws Ioexcep tion {File File = new file ("D:" +file.separator+ "io" +file.separator+ "a letter to the novice programmer". txt "); if (!file.exists ()) { File.createnewfile (); System.out.println ("file created"); System.out.println ("Name:" + file.getname ()); SYSTEM.OUT.PRINTLN ("Relative path:" + File.getcanonicalpath ()); SYSTEM.OUT.PRINTLN ("absolute path:" + File.getabsolutepath ()); SYSTEM.OUT.PRINTLN ("File size:" + file.length ()), if (File.delete ()) {System.out.println ("file deleted!");}}}
FileInputStream and FileOutputStream Use these two to output the corresponding text file content, using InputStreamReader and outputstreamwriter to input and output file streams, can also be used to convert the encoding method, This allows the output file to be garbled.
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter; Public classTest02 { Public Static voidMain (string[] args) {String src= "D:" + file.separator + "IO" +File.separator+ "Hello.txt"; String dest= "D:" + file.separator + "IO" + file.separator + "out" + file.separator + "Hello.txt"; FileInputStream in=NULL; FileOutputStream out=NULL; File Path=NewFile (dest); Try { if(!path.exists ()) {Path.createnewfile (); } } Catch(IOException e) {e.printstacktrace (); } Try{ in=NewFileInputStream (SRC); out=NewFileOutputStream (dest); InputStreamReader is=NewInputStreamReader (In, "GBK"); OutputStreamWriter OS=NewOutputStreamWriter (out, "GBK"); //Read File contents//byte[] buf = new byte[1024]; //int len = 0; //While (len = In.read (buf))!=-1) {//System.out.println (New String (Buf,0,len)); // } Char[] buf =New Char[1024]; intLen = 0; while(len = Is.read (BUF))! =-1) { //System.out.println (New String (Buf,0,len));Os.write (NewString (buf, 0, Len)); Os.flush (); } } Catch(IOException e) {e.printstacktrace (); } finally { if(In! =NULL) { Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } if(In! =NULL) { Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Use BufferedReader and bufferedwriter to read or write content using buffers
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter; Public classTest03 { Public Static voidMain (string[] args) {String src= "D:" + file.separator + "IO" + file.separator + "Pet.txt"; String dest= "D:" + file.separator + "IO" +File.separator+ "Pets.txt"; FileReader FR=NULL; FileWriter FW=NULL; File Path=NewFile (dest); if(!path.exists ()) { Try{path.createnewfile (); } Catch(IOException e) {e.printstacktrace (); }} bufferedreader br=NULL; BufferedWriter BW=NULL; Try{FR=NewFileReader (SRC); FW=NewFileWriter (dest); //to TranscodeBR =NewBufferedReader (NewInputStreamReader (NewFileInputStream (SRC),"GBK")); Bw=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (Path), "GBK")); String Str=NULL; System.out.print ("Before replacing:"); Char[] ch =New Char[1024]; inttemp = 0; while(temp = br.read (ch))! =-1) {str=NewString (CH, 0, temp); System.out.println (str); } System.out.print ("After replacement:"); String name= "Euro", type = "Dog", master = "Fanfan"; String One= Str.replace ("{Name}", name); String of= One.replace ("{type}", type); String there= Two.replace ("{Master}", master); System.out.println (there); Bw.write (there); } Catch(IOException e) {e.printstacktrace (); } finally { if(BR! =NULL) { Try{br.close (); Fr.close (); } Catch(IOException e) {e.printstacktrace (); } } if(BW! =NULL) { Try{bw.close (); Fw.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Use FileInputStream and FileOutputStream for reading and input of binary files, or you can use buffers to read and output bufferedinputstream and bufferedoutputstream binary files
ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classTest04 { Public Static voidMain (string[] args) {//under Windows add \ \ Linux under Add \String src = "D:" + file.separator + "IO" + file.separator + "1.jpg"; String dest= "D:" + file.separator + "IO" + file.separator + "out" + file.separator + "1.jpg"; FileInputStream I=NULL; FileOutputStream o=NULL; Bufferedinputstream Bi=NULL; Bufferedoutputstream Bo=NULL; Try{i=NewFileInputStream (SRC); o=NewFileOutputStream (dest); Bi=NewBufferedinputstream (i); Bo=NewBufferedoutputstream (o); byte[] buf =New byte[1024]; inttemp = 0; while(temp = Bi.read (BUF))! =-1) {bo.write (buf,0, temp); } } Catch(IOException e) {e.printstacktrace (); } finally { if(Bi! =NULL) { Try{bi.close (); I.close (); } Catch(IOException e) {e.printstacktrace (); } } if(Bo! =NULL) { Try{bo.close (); O.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
In addition to the char[] and byte[] array for reading data, char[] Most of the time for reading text content and byte[] for binary file reading
//This part of the code is in Test03Char[] ch =New Char[1024]; inttemp = 0; while(temp = br.read (ch))! =-1) {str=NewString (CH, 0, temp); System.out.println (str); }//This part of the code is in Test04byte[] buf =New byte[1024]; inttemp = 0; while(temp = Bi.read (BUF))! =-1) {bo.write (buf,0, temp);}
The summary is probably so much, there are several other ways to read the contents of the file, please see this article:
Http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.html
JAVA IO Summary