Basic File operations-InputStream byte input stream

Source: Internet
Author: User

I directly pasted the code. Please leave me a message if you have any questions.

Package org. mark. streamRW; import java. io. file; import java. io. fileInputStream; import java. io. inputStream;/*** byte output stream: OutputStream, the maximum parent class of the byte output stream in the entire IO package * byte input stream: InputStream ** InputStream uses the subclass FileInputStream. Read */public class InStream1 {/*** @ param args */public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 1: instantiate the parent class Object InputStream in = null through subclass; in = new FileInputStream (file); // perform object polymorphism, instantiate // 3. read byte [] B = new byte [1024]; in. read (B); // 4. disable in. close (); System. out. println (new String (B); // convert it to String output // Legacy problem: many spaces are left. It is not that big. It has opened 1024 space and NO content is Blank }}

Legacy issues of the previous program:

Package org. mark. streamRW; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; // solves the remaining issues: public class InStream2 {/*** @ param args */public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 4: instantiate the parent class Object InputStream in = null through subclass; in = new FileInputStream (file); // Input Row instantiation // 3. read byte [] B = new byte [1024]; int len = in. read (B); // read the content // 4. disable in. close (); // solve the remaining issues. Refer to the method System provided by String. err. println ("length of data read:" + len); System. out. println (new String (B, 0, len); // convert it to String output // can I open up space based on the file size? }}

Solve the Problem of opening up space based on the file size

Package org. mark. streamRW; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; // solves the problem of opening up space public class InStream3 {/*** @ param args */public static void main (String [] args) based on the file size) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 1: instantiate the parent class Object InputStream in = null through subclass; in = new FileInputStream (file); // perform object polymorphism, instantiate // 3. read byte [] B = new byte [(int) file. length ()]; // solves the problem of opening up space int len = in according to the file size. read (B); // read the content // 4. disable in. close (); // solve the remaining issues. Refer to the method System provided by String. err. println ("length of data read:" + len); System. out. println (new String (B); // convert to String output }}

Only suitable for input that knows the file size

Package org. mark. streamRW; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; // only applicable to input public class InStream4 {/*** @ param args */public static void main (String [] args) that knows the file size) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 1: instantiate the parent class Object InputStream in = null through subclass; in = new FileInputStream (file); // perform object polymorphism, instantiate // 3. read byte [] B = new byte [(int) file. length ()]; // solves the problem of opening up space for (int I = 0; I <B. length; I ++) {B [I] = (byte) in. read ();} // int len = in. read (B); // read the content // 4. disable in. close (); // solve the remaining issues. Refer to the method System provided by String. out. println (new String (B); // convert to String output }}

When you do not know the size of the read content, you can only read the data as-1.

Package org. mark. streamRW; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; // when you do not know the size of the read content, the public class InStream5 {/*** @ param args */public static void main (String [] args) Mark can only be read with the value of-1) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 4: instantiate the parent class Object InputStream in = null through subclass; in = new File InputStream (file); // instantiate the object through object polymorphism. // 3. read byte [] B = new byte [1024]; // solve the problem of opening up space int len = 0 according to the file size; int temp = 0; // accept each read data while (temp = in. read ())! =-1) {// indicates that there is still content. The file has not been read. B [len] = (byte) temp; len ++;} // 4. disable in. close (); // solve the remaining issues. Refer to the method System provided by String. out. println (new String (B, 0, len); // convert to String output }}


Posting is too fast ~ Close the program ~

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.