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 = 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: leave a lot of spaces. 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 = 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); // It is converted into a 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 = 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 based on 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 = 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 just mark the read 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) throws exception {// todo auto-generated method stub // Step 1: use the File class to find a file = new file ("D:" + file. separator + "test.txt"); // Step 4: instantiate the parent class Object inputstream in = NULL through subclass; In = new fil Einputstream (File); // instantiate the object through object polymorphism. // 3. read byte [] B = new byte [1024]; // solve the problem of opening up space int Len = 0 based on 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 ~ Almost the program is closed ~