java io Stream is the input and output stream, the stream is a set of sequential, with the beginning and end of the byte combination, is the general name of the data transmission. That is, the transmission of data between two devices is called a stream, and the nature of the flow is data transmission. the IO stream can be divided into byte stream and character stream. Give the corresponding IO structure diagram:in the next period of time, will slowly introduce the use of various streams, this blog first introduce the fileoutputstream of the byte stream and the corresponding FileInputStream. I. FileOutputStream (file output stream)OutputStream is an abstract class, and an abstract class must be implemented by subclasses. Now the output to the file is going to use FileOutputStream. The FileOutputStream has four construction methods, respectively1.FileOutputStream (file file)-------------writing data to files of file objects2.FileOutputStream (file File,boolean append);------Append write data to file object3.FileOutputStream (String path)-------------writing data to the specified file4.FileOutputStream (String path,boolean append);--------Append write data to the specified fileWhen the value of append is true, data written to the file is appended to the original data, or the file's data is overwritten. The default is False. Write Method 1: Byte writes
- public static void Main (string[] args) {
- try {
- Create a file byte output stream object
- OutputStream os=new FileOutputStream ("L:\\io.txt");
- Data being written
- String string= "Hello IO Stream";
- Byte[]bytes=string.getbytes ();//Convert to byte array
- for (int i=0;i<bytes.length;i++) {
- Output to File
- Os.write (Bytes[i]);
- }
- Os.close ();//Close stream
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy Codemethod Two: Write all at once
- public static void Main (string[] args) {
- try {
- Create a file byte output stream object
- OutputStream os=new FileOutputStream ("L:\\io.txt", true);//Append
- Data being written
- String string= "Hello IO Stream";
- Byte[]bytes=string.getbytes ();//Convert to byte array
- Os.write (bytes);//Write All
- Os.write (bytes,0,5) indicates a write length of 5 bytes starting from 0
- Os.close ();//Close stream
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy CodeI. FileInputStream (file input stream)FileInputStream is the input byte obtained from a file in the system, with two construction methods1.FileInputStream (file file)2.FileInputStream (String path) Read Byte Method 1: Byte read
- public static void Main (string[] args) {
- try {
- Create a byte input stream object
- InputStream is=new FileInputStream ("L:\\io.txt");
- int b=-1;
- while ((B=is.read ())!=-1)//byte read, when 1 is complete
- {
- System.out.print ((char) b);//convert to character output
- }
- Is.close ();//Close stream
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy Codethe output is: Hello IO Stream. Such a one-byte read is very slow. Read Byte Method 2: Read all bytes at once
- public static void Main (string[] args) {
- try {
- File File=new file ("L:\\io.txt");
- Create a byte input stream object
- InputStream is=new fileinputstream (file);
- To create a byte array based on the file size
- Byte[]bytes=new byte[(int) file.length ()];
- int Len=is.read (bytes);//Returns the length of the read byte
- System.out.println ("read byte length:" +len);
- SYSTEM.OUT.PRINTLN ("reads:" +new string (bytes));//build into string output
- Is.close ();//Close stream
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy CodeOperation Result:<ignore_js_op>
The main disadvantage of this method of reading is to build a byte array as large as the file size, when the file is small, and when the file is large, memory may not be able to frame such a large array of bytes. Therefore, this method is only suitable for small files. Read Byte Method 3: Read the specified length each time (the most common method)
- public static void Main (string[] args) {
- try {
- Create a byte input stream object
- InputStream is=new FileInputStream ("L:\\io.txt");
- Specify the size of each read--can be modified based on performance bytes
- Byte[]bytes=new Byte[8];
- StringBuffer sb=new StringBuffer ();
- int len=-1;//The actual length of each read
- while ((Len=is.read (bytes))!=-1)
- {
- Sb.append (New String (Bytes,0,len));
- }
- System.out.println ("Read bytes:" +SB);
- Is.close ();//Close stream
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
Copy CodeOutput Result: <ignore_js_op> |
"FileInputStream and FileOutputStream of IO streams in Java"