"FileInputStream and FileOutputStream of IO streams in Java"

Source: Internet
Author: User

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
  1. public static void Main (string[] args) {
  2. try {
  3. Create a file byte output stream object
  4. OutputStream os=new FileOutputStream ("L:\\io.txt");
  5. Data being written
  6. String string= "Hello IO Stream";
  7. Byte[]bytes=string.getbytes ();//Convert to byte array
  8. for (int i=0;i<bytes.length;i++) {
  9. Output to File
  10. Os.write (Bytes[i]);
  11. }
  12. Os.close ();//Close stream
  13. } catch (FileNotFoundException e) {
  14. E.printstacktrace ();
  15. } catch (IOException e) {
  16. E.printstacktrace ();
  17. }
  18. }
Copy Codemethod Two: Write all at once
  1. public static void Main (string[] args) {
  2. try {
  3. Create a file byte output stream object
  4. OutputStream os=new FileOutputStream ("L:\\io.txt", true);//Append
  5. Data being written
  6. String string= "Hello IO Stream";
  7. Byte[]bytes=string.getbytes ();//Convert to byte array
  8. Os.write (bytes);//Write All
  9. Os.write (bytes,0,5) indicates a write length of 5 bytes starting from 0
  10. Os.close ();//Close stream
  11. } catch (FileNotFoundException e) {
  12. E.printstacktrace ();
  13. } catch (IOException e) {
  14. E.printstacktrace ();
  15. }
  16. }
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
  1. public static void Main (string[] args) {
  2. try {
  3. Create a byte input stream object
  4. InputStream is=new FileInputStream ("L:\\io.txt");
  5. int b=-1;
  6. while ((B=is.read ())!=-1)//byte read, when 1 is complete
  7. {
  8. System.out.print ((char) b);//convert to character output
  9. }
  10. Is.close ();//Close stream
  11. } catch (FileNotFoundException e) {
  12. E.printstacktrace ();
  13. } catch (IOException e) {
  14. E.printstacktrace ();
  15. }
  16. }
Copy Codethe output is: Hello IO Stream. Such a one-byte read is very slow.
Read Byte Method 2: Read all bytes at once
  1. public static void Main (string[] args) {
  2. try {
  3. File File=new file ("L:\\io.txt");
  4. Create a byte input stream object
  5. InputStream is=new fileinputstream (file);
  6. To create a byte array based on the file size
  7. Byte[]bytes=new byte[(int) file.length ()];
  8. int Len=is.read (bytes);//Returns the length of the read byte
  9. System.out.println ("read byte length:" +len);
  10. SYSTEM.OUT.PRINTLN ("reads:" +new string (bytes));//build into string output
  11. Is.close ();//Close stream
  12. } catch (FileNotFoundException e) {
  13. E.printstacktrace ();
  14. } catch (IOException e) {
  15. E.printstacktrace ();
  16. }
  17. }
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)
  1. public static void Main (string[] args) {
  2. try {
  3. Create a byte input stream object
  4. InputStream is=new FileInputStream ("L:\\io.txt");
  5. Specify the size of each read--can be modified based on performance bytes
  6. Byte[]bytes=new Byte[8];
  7. StringBuffer sb=new StringBuffer ();
  8. int len=-1;//The actual length of each read
  9. while ((Len=is.read (bytes))!=-1)
  10. {
  11. Sb.append (New String (Bytes,0,len));
  12. }
  13. System.out.println ("Read bytes:" +SB);
  14. Is.close ();//Close stream
  15. } catch (FileNotFoundException e) {
  16. E.printstacktrace ();
  17. } catch (IOException e) {
  18. E.printstacktrace ();
  19. }
  20. }
Copy CodeOutput Result:
<ignore_js_op>

"FileInputStream and FileOutputStream of IO streams in Java"

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.