This section review Java Common I/O, input and output streams.
First put the sample code, convenient reference, can be easily read.
PackageRe08;ImportJava.io.*;ImportJava.util.Scanner; Public classIotest { Public Static voidMain (string[] args) {File file=NewFile ("D:/1.txt");//File Creation if(File.exists ()) {file.delete (); } Else { Try{file.createnewfile (); } Catch(IOException e) {e.printstacktrace (); } } Try{FileOutputStream out=NewFileOutputStream (file);//Output to File byteByteout[] = "It ' s a test.". GetBytes (); Out.write (byteout); Out.close (); } Catch(Exception e) {e.printstacktrace (); } Try{FileInputStream in=NewFileInputStream (file);//read in file contents byteBytein[] =New byte[1024]; intLen =In.read (Bytein); System.out.println ("The message is:" +NewString (Bytein, 0, Len)); In.close (); } Catch(Exception e) {e.printstacktrace (); } Scanner SC=NewScanner (system.in);//Scanner Practice FamiliarString line = Sc.nextline ();//ReadingString line2 =Sc.next (); inti = Sc.nextint ();//read in int DoubleD = sc.nextdouble ();//read in doubleSystem.out.println (line + line2 + i +d); }}
FileInputStream, FileOutputStream are read into the file, output to a file, the parameter is file type, that is, a document storage path (including file name)
By method: Read, write can realize reading and writing.
In addition the commonly used read-in characters from the keyboard are the scanner class.
Through the method: Nextline (), Nextint () and other reading.
Java Review (8)---I/o