Java io (iii) inputstream outputstream class

Source: Internet
Author: User

First, both classes are abstract classes and must be instantiated through subclass objects.

Second, these two classes are both byte operation classes and need to be usedByteArray operation data. There must be an array size limit.

Example1: Write a string to the file.

Import Java. io. *; public class oodemo04 {public static void main (string [] ARGs) throws exception {// 1g. to operate the gzg.txt file F = new file ("E: // gzg.txt "); // 2. instantiate by subclass // use the fileoutputstream class outputstream out = NULL; out = new fileoutputstream (f); string STR = "Hello gzg... hello "; string str1 = Str. tolowercase (); byte [] B = str1.getbytes (); // 3. Write the byte array to the file out. write (B); // 4. disable data flow out. close ();}}

Example2: Read data from a file.

Import Java. io. *; public class oodemo05 {public static void main (string [] ARGs) {// 1. construct a subclass object file to find the file F = new file ("E: // gzg.txt"); // 2. and instantiate the parent class inputstream in = NULL through file; try {In = new fileinputstream (f);} catch (exception e) {system. out. println ("An error occurred while opening the file !!! ");} // 3. read data from the file // use the int read (byte [] B) method in the parent class to read data from the byte array. The returned value is the number of bytes read. intlen = 0; byte [] B = newbyte [1024]; try {Len = in. read (B);} catch (ioexception e) {e. printstacktrace ();} // convert the data read to the byte array to the string type, and print the output string STR = new string (B); system. out. println ("read content:" + Str); // 4. close the input stream try {In. close ();} catch (ioexception e) {e. printstacktrace ();}}}

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.