Io stream FileInputStream and FileOutputStream read and write __java

Source: Internet
Author: User
I. How to read and write files reads a file through a stream. A stream is a series of flowing characters that send an information channel in a first-in, first-out manner. ii. Classification of Java streams to divide by the flow direction 1. Output stream OutputStream and writer as base class 2. Input stream InputStream and reader as base class input and output are relative to computer memory. divided by processing data units 1, Byte stream ⑴ byte input stream, InputStream base class; ⑵ byte output stream, OutputStream base class; 2. Character Streams ⑴ character input stream reader base class ⑵ character output stream writer base class Note: 1 characters = 2 bytes The byte is a 8-bit universal byte stream, and the character streams are 16-bit Unicode character streams. third, the use of byte stream and character streams 1 character streams: Reading for text documents 2 bytes: Pictures, audio, video. Ensure that the distortion is minimized. use FileInputStream class to read text files implementation Steps


eg: 1, import some bags. 2, FileInputStream fi=new fileinputstream ("C\\test.txt") 3, fi.available (); Fi.read (); 4, Fi.close (); v. Read text files using the FileOutputStream class

eg: 1, import some bags 2, FileOutputStream fos=new fileoutstream ("C\\test.test"); String str= "123456"; byte[] S=str.getbyte (); Fos.write (s); 3, Fos.close (); Six, the commonly used method common methods of ⒈inputstream and its subclasses FileInputStream ⑴, class name. Read (), receiving with type int reads a byte of data from this input stream. ⑵, class name. read (byte[] b); receive with type int reads up to b.length bytes of data from this input stream into a byte array. ⑶, class name. Read (Byte[]b,int off,int len); receive with type int reads up to len bytes of data from this input stream into a byte array. ⑷, class name. Close (); closes this file input stream and releases all system resources associated with this stream. ⑸, class name. Available (), receiving with type int returns the estimated remaining bytes of the next method that is invoked on this input stream that can be read (or skipped) from this input stream without blocking common construction methods of subclass FileInputStream ⑴, FileInputStream (file file); ⑵, FileInputStream (String name); 2, OutputStream and its subclass FileOutputStream common methods ⑴, class name. write (int c); writes the specified byte to this file output stream. ⑵, class name. write (btye[]b); writes B.length bytes from the specified byte array to this file output stream. ⑶, class name. write (byte[] b, int off, int len); writes the Len byte of the specified byte array from the offset off to this file output stream. common construction methods of subclass FileOutputStream fileoutputstream (file file); FileOutputStream (String name); FileOutputStream (String name,boolean append); Note: The first two constructs will overwrite the contents of the file when they write data to the file When a FileOutputStream instance is created, a new, empty file is automatically created if the corresponding file does not exist. Example: Copy the contents of the text Document of e-disk to the C disk Public static void Main (String[]args) { try { fileinputstream in=new fileinputstream ("E:\\ee\\title.txt"); fileoutputstream out=new fileoutputstream ("c:\\ copy. txt");              byte[]b=new byte[12]; int len=0; While ((Len=in.read (b))!=-1) { Out.write (b, 0, Len);             }              in.close (); out.close (); catch (Exception e) {              e.printstacktrace ();         }






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.