Java IO Node Stream fileinput/outputstream

Source: Internet
Author: User

Java IO Node Stream fileinput/outputstream

@author Ixenos

File stream of the node stream

File read and write is the most common I/O operation, through the file stream to connect disk files, read and write file contents

1. file Read and write workflow:

1) Open file input stream or output stream

FileInputStream implements a read file, the constructor that invokes FileInputStream can open a file input stream:

1 public fileinputstream (String filename) throws FileNotFoundException//Specify File name 2 public fileinputstream (file file) throws FileNotFoundException//Specifies a file object 3 public fileinputstream (FileDescriptor fdobj)//requires a FileName descriptor object

FileDescriptor http://www.fengfly.com/plus/view-214059-1.html

If you attempt to open a file input stream on a nonexistent file, the constructor throws an exception filenotfoundexception, which is a subclass of IOException

  most commonly used : Open a file output stream by file name try{fileinputstream fin = new FileInputStream ("Readme.txt");} catch (IOException e) {...}

2) file read or write operations

FileInputStream

1 public native int read () throws IOException2 public int read (byte[] data) throws IOException3 public int read (byte[] Dat A, int offset, int length) throws IOException

If for some reason the file is unreadable, the Read method throws IOException

3) close file input stream or output stream

1 ...  2  finally{3      try{4          //Because in the finally module, there is no stream if the file does not exist, so there is a null pointer to determine the 5          if (fin! = null) {6               fin.close (); 7           }  8      }catch (Exception e) {} 9  }10 ...  -------------------------------12 because close () can also produce an exception, the code is more miscellaneous 13 you can use Try-with-resources to automatically close the flow http://www.cnblogs.com /ixenos/p/5701679.html

4) FileInputStream corresponding to the FileOutputStream implementation of the file output function

1 public fileoutputstream (string name) throws FileNotFoundException2 public FileOutputStream (string name, Boolean append ) Throws FileNotFoundException

(1) when the first constructor is called, if the file specified by name does not exist, the file is created and an output stream is established, and if present, the contents of the file are overwritten

(2) when the second constructor is called, the second parameter append specifies whether to overwrite the existing file, and if Append is true, the new content is added at the end of the file, and if False, the contents of the file are overwritten

(3) FileOutputStream constructor can create a new file at the same time, open an output stream to write, which is not createnewfile () of the file object

Write  Public void Write (byte[] b)           throws  ioexception    byte The array is written to this output stream. The general contract for write (b) is: Write with the call (b,0– If an I/O error occurs.
api-write1

Write Public voidWritebyte[] B,intoff,intlen)throwsIOException will specifybyteThis output stream is written to the Len byte of the array starting at offset off. The general contract for write (b, off, Len) is to write some bytes in array b sequentially to the output stream; element B[off] is the first byte written by this operation, b[off+len-1] is the last byte written by this operation. The Write method of the OutputStream invokes the write method of a parameter on each byte to be written out. It is recommended that subclasses override this method and provide a more efficient implementation. If B isNULL, the NullPointerException is thrown. If off is negative, or Len is negative, or off+Len is greater than the length of array B, the indexoutofboundsexception is thrown. Parameter: b-data. Off-the initial offset in the data. Len-the number of bytes to write. API-outputstream-write
Api-write

Java IO Node Stream fileinput/outputstream

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.