Fileinputstream
Import Java. io. file; import Java. io. fileinputstream; import Java. io. inputstream; public class testfileinputstream {public static void main (string [] ARGs) throws exception {// The exception is thrown and not processed. // Step 3: use the file class to find a file F = new file ("C:" + file. separator + "test.txt"); // declare the file object // Step 2: instantiate the parent class Object inputstream input = NULL through subclass; // prepare an input object, instantiate input = new fileinputstream (f) Through object polymorphism; // Step 1: read all the content and read byte B [] = new byte [3rd] In this array; int Len = input. read (B); // Step 2: Close the input stream input. close (); // convert the byte array into a string and output the system. out. println ("length of data read:" + Len); system. out. println ("content:" + new string (B, 0, Len ));}}
Fileoutputstream
Import Java. io. file; import Java. io. fileoutputstream; import Java. io. outputstream; public class testfileoutputstream {public static void main (string [] ARGs) throws exception {// The exception is thrown and not processed. // Step 3: use the file class to find a file F = new file ("C:" + file. separator + "test.txt"); // declare the file object // Step 2: instantiate the parent class Object outputstream out = NULL through the subclass; // prepare an output object and use the object polymorphism, instantiate out = new fileoutputstream (f); // Step 4: Write a string. STR = "Hello world !!! "; // Only the byte array can be output, so the string is changed to byte array byte B [] = Str. getbytes (); // output the content and save the file out. write (B); // Step 4: Close the output stream out. close ();}}
Filereader
Import Java. io. file; import Java. io. filereader; import Java. io. reader; public class testfilereader {public static void main (string [] ARGs) throws exception {// exception thrown, not processed // Step 4: Use the file class to locate a file, declare file object file F = new file ("D:" + file. separator + "test.txt"); // Step 2: instantiate the parent class Object reader = NULL through subclass; // prepare an input object, instantiate reader = new filereader (f) Through object polymorphism; // Step 1: read all the content and read the char C [] = new char [3rd] In this array; int Len = reader. read (c); // Step 4: Close the input stream reader. close (); // convert the char array into a string and output the system. out. println ("content:" + new string (C, 0, Len ));}}
Filewriter
Import Java. io. file; import Java. io. filewriter; import Java. io. writer; public class testfilewriter {public static void main (string [] ARGs) throws exception {// exception thrown, not processed // Step 1: use the File class to locate a file, declare file object file F = new file ("C:" + file. separator + "test.txt"); // Step 2: instantiate the parent class Object writer out = NULL through subclass; // prepare an output object through object polymorphism, instantiate out = new filewriter (f); // Step 4: Write a string STR = "Hello world !!! "; Out. Write (STR); Out. Flush (); // Step 3: Close output stream out. Close ();}}