input stream InputStream(See the last one.)
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 6 Public classFileIO {7 Public Static voidMain (string[] args)throwsIOException {8String filename= "D:" +file.separator+ "Io.txt";9File file=NewFile (FileName);TenInputStream inputstream=Newfileinputstream (file); One byte[] by=New byte[1024]; A Inputstream.read (by); - inputstream.close (); -System.out.println (NewString (by)); the } -}
Note: In this example, because the B-byte array has a length of 1024, if the file is small, there will be a large number of padding spaces. We can use In.read (b); The return value to design the program, as in the following case:
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 6 Public classFileIO {7 Public Static voidMain (string[] args)throwsIOException {8String filename= "D:" +file.separator+ "Io.txt";9File file=NewFile (FileName);TenInputStream inputstream=Newfileinputstream (file); One byte[] by=New byte[1024]; A intlen=Inputstream.read (by); - inputstream.close (); -SYSTEM.OUT.PRINTLN ("read-in length:" +len); theSystem.out.println (NewString (by)); - } -}
Note: As you can see from the example above, we have previously applied for a specified size space, but sometimes the space may be too small, sometimes too large, and we need the exact size to save space, so we can do this:
1 ImportJava.io.File;2 ImportJava.io.FileInputStream;3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 6 Public classFileIO {7 Public Static voidMain (string[] args)throwsIOException {8String filename= "D:" +file.separator+ "Io.txt";9File file=NewFile (FileName);TenInputStream inputstream=Newfileinputstream (file); One byte[] by=New byte[(int) File.length ()]; A intlen=Inputstream.read (by); - inputstream.close (); -SYSTEM.OUT.PRINTLN ("read-in length:" +len); theSystem.out.println (NewString (by)); - } -}
Note: The above example is to know how large the contents of the file, and then to expand, and sometimes we do not know how big the file, in this case, we need to determine whether the original file at the end.
1 Public classFileIO {2 Public Static voidMain (string[] args)throwsIOException {3String filename= "D:" +file.separator+ "Io.txt";4File file=NewFile (FileName);5InputStream inputstream=Newfileinputstream (file);6 byte[] by=New byte[1024];7 intCount=0;8 intTemp=0;9 while((Temp=inputstream.read ())!=-1){TenBy[count++]= (byte) temp; One A } - inputstream.close (); -SYSTEM.OUT.PRINTLN ("read-in length:" +file.length ()); theSystem.out.println (NewString (by)); - } -}
Note: When the end of the file is read, it returns-1. Normally, it will not return-1.
output Stream OutputStream
1 Public classFileIO {2 Public Static voidMain (string[] args)throwsIOException {3String filename= "D:" +file.separator+ "Io.txt";4File file=NewFile (FileName);5OutputStream out=Newfileoutputstream (file); 6String str= "Hello,io";7 byte[] by=str.getbytes ();8 Out.write (by);9 out.close ();Ten } One}
Note: The previous content will be overwritten
1 Public classFileIO {2 Public Static voidMain (string[] args)throwsIOException {3String filename= "D:" +file.separator+ "Io.txt";4File file=NewFile (FileName);5OutputStream out=NewFileOutputStream (file,true);//true indicates append mode, otherwise overrides6String str= "\r\nhello,io";//line Break7 byte[] by=str.getbytes ();8 Out.write (by);9 out.close ();Ten } One}
Java IO stream