Io stream-byte stream

Source: Internet
Author: User

tag: blog Java OS file IO Re

Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. linenumberreader; import Java. NIO. buffer; public class Io {public static void main (string [] ARGs) throws ioexception {writefile (); readfile1 (); readfile2 ();} // Write Public static void writefile () Throws ioexception {fileoutputstream Fos = new fileoutputstream ("fos.txt"); FOS. write ("ABCD ". getbytes (); FOS. close () ;}// read public static void readfile1 () throws ioexception {fileinputstream fis1 = new fileinputstream ("fos.txt"); int CH = 0; while (CH = fis1.read ())! =-1) // read {system. out. println (char) CH);} fis1.close ();} public static void readfile2 () throws ioexception {fileinputstream fis2 = new fileinputstream ("fos.txt "); byte [] Buf = new byte [1024]; // buffer int Len = 0; while (LEN = fis2.read (BUF ))! =-1) // read the returned value in array size is the number of reads {system. out. println (new string (BUF, 0, Len);} fis2.close (); fileinputstream fis3 = new fileinputstream ("fos.txt "); // number of bytes in the fis2.available file int num = fis3.available (); // defines a buffer that is just right, memory byte [] buf2 = new byte [fis3.available ()]; fis3.read (buf2); system. out. println (Num + ":" + new string (buf2); fis3.close ();}}

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.