First of all, why doesn't Java IO call a file called Io? Io is actually the abstraction of a file or a network, abstract into a "stream", "stream" is a one-dimensional, and is hi one-way flow: From the file read the data is a stream, write to the file is also a stream, read data from the network is a stream, write data to the network is also a stream and so on.
According to the data to divide can be divided into input stream, output stream
The type of data processed can be divided into character stream and byte stream
First of all to distinguish Java Io object, compare itself to a program, write data to the program is input operation, from the program outward output data is the output operation
JAVA io is roughly divided into byte streams and character streams. BYTE stream: Data when binary, human unreadable, with InputStream, OutputStream interface. Characters stream: Data is character-based, readable, and has a reader,writer interface.
Inheritance relationships for various streams:
Anyway, the first time I saw a headache, so many classes are not clear, do not understand. But commonly used have filereader, BufferedReader, Filewriter,fileinputstream, FileOutputStream. Refer to API documentation for specific use
Following the actual situation, write the code to see how the specific interface is used
File F = new file ("Src/java/io/readmyself.java");
System.out.println (F.exists ()); Determine if the current file exists
System.out.println (F.getabsolutepath ()); Gets the absolute path of the current file F:\JavaLearning\src\Java\io\ReadMySelf.java
Such operations as F.canread (), F.canwrite (), F.canexcute (), F.creatnewfile () will determine if the current file exists and will generate a new file if it does not exist.
file[] files = f.listfiles (); Files can be traversed to all file directories under the current file
The following shows a character stream to read the file:
Public classreadmyself { Public Static voidMain (string[] args)throwsException {File f=NewFile ("Src/java/io/readmyself.java"); FileReader FR=NULL; Try{FR=NewFileReader (f); Char[] buffer =New Char[10]; intLength =-1; while(length = fr.read (buffer))! =-1) {//reads 10 bytes at a time if(Length = = 10) {System.out.print (buffer); }Else{System.out.println (NewString (buffer, 0, length)); }}}Catch(Exception e) {e.printstacktrace (); } finally{if(FR! =NULL) {fr.close (); } }
}
You can also use BufferedReader to read by line
Public classreadmyself { Public Static voidMain (string[] args)throwsException {File f=NewFile ("Src/java/io/readmyself.java"); BufferedReader BR=NULL; Try{br=NewBufferedReader (NewFileReader (f)); String Line=NULL; while(line = Br.readline ())! =NULL) {System.out.println (line); } } Catch(Exception e) {e.printstacktrace (); } finally { if(BR! =NULL) {br.close (); } }}
Write data using Stringwriter/filewriter, where StringWriter is used
Public classreadmyself { Public Static voidMain (string[] args)throwsException {File f=NewFile ("Src/java/io/readmyself.java"); StringWriter SW=NewStringWriter (); BufferedReader BR=NULL; Try{br=NewBufferedReader (NewFileReader (f)); String Line=NULL; while(line = Br.readline ())! =NULL) {sw.write (line); Sw.write ("\ n"); } System.out.println (Sw.tostring ()); } Catch(Exception e) {e.printstacktrace (); } finally { if(BR! =NULL) {br.close (); } }
}
The following uses a byte stream to read a picture and write to the local program folder:
Public classreadmyself { Public Static voidMain (string[] args)throwsException {Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try { byte[] buffer =New byte[1024]; File F=NewFile ("F:/javalearning/bd_logo.png"); System.out.println (F.exists ()); Bis=NewBufferedinputstream (NewFileInputStream ("F:/javalearning/bd_logo.png")); Bos=NewBufferedoutputstream (NewFileOutputStream ("Src/java/io/a.png")); intLength =-1; while(length = bis.read (buffer))! =-1) {bos.write (buffer,0, length); } bos.flush (); } Catch(Exception e) {}finally{bis.close (); Bos.close (); }}
All of these are sequential read and write files, you can also use Randomaccessfile random Read and write files, there are two modes "" RW "" "" R "" (Read Only)
Randomaccessfile raf=new randomaccessfile (file, "RW")
The internal contains a file pointer, the opening of the file pointer at the beginning of the pointer=0, read and write operations, the pointer will move backwards, using seek (int index); Can manipulate pointers
Public classreadmyself { Public Static voidMain (string[] args)throwsException {randomaccessfile file=NewRandomaccessfile ("Src/java/io/readme.txt", "RW");//If the file does not exist, it is created locallyFile.writeint (20);//accounted for 4 bytesFile.writedouble (3.1415926);//accounted for 8 bytesFile.writeutf ("This is a UTF string");//This length is written in the first two bytes of the current file pointer and can be read with Readshort ()File.writeboolean (true);//accounted for 1 bytesFile.writeshort (155);//accounted for 2 bytesFile.writelong (1563231521);//accounted for 8 bytesFile.writeutf ("Again a UTF string"); File.writefloat (35.12F);//accounted for 4 bytesFile.writechar (' a ');//accounted for 2 bytesFile.seek (0);//set the file pointer at the beginning of the file//The following reads the data from the file files, paying attention to the pointer positionSystem.out.println ("-----------reading data from a file file specified location--------"); System.out.println (File.readint ()); System.out.println (File.readdouble ()); System.out.println (File.readutf ()); File.skipbytes (3);//file pointer skips three bytesSystem.out.println (File.readlong ());}
The above is the Java IO to do their own notes, there are many shortcomings, I still need to work hard.
JAVA Foundation-java IO