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 ();}}}