FileInputStream
- <span style="Font-family:verdana;" >import java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.InputStream;
- Public class Testfileinputstream {
- public static void Main (string[] args) throws Exception { //exception thrown, not processed
- //1th step: Use the file class to find a document
- File F = new file ("C:" + File.separator + "test.txt"); Declaring a File object
- //2nd step: Instantiating a parent class object from a subclass
- InputStream input = null;
- //Prepare an input object for instantiation by object polymorphism
- input = new FileInputStream (f);
- //3rd step: Read, all the contents are read in this array
- byte b[] = new byte[1024];
- int len = Input.read (b);
- //4th step: Turn off the input stream
- Input.close ();
- //change byte array to string output
- System.out.println ("Length of the read-in data:" + len);
- System.out.println ("content:" + new String (b, 0, Len));
- }
- }</span>
FileOutputStream
- <span style="Font-family:verdana;" >import java.io.File;
- Import Java.io.FileOutputStream;
- Import Java.io.OutputStream;
- Public class Testfileoutputstream {
- public static void Main (string[] args) throws Exception { //exception thrown, not processed
- //1th step: Use the file class to find a document
- File F = new file ("C:" + File.separator + "test.txt"); //Declare file object
- //2nd step: Instantiating a parent class object from a subclass
- OutputStream out = null;
- //Prepare an Output object, and instantiate it through the polymorphism of the object
- out = new FileOutputStream (f);
- //3rd step: Write, prepare a string
- String str = "Hello world!!!";
- //can only output a byte array, so the string becomes a byte array
- byte b[] = Str.getbytes ();
- //Output The content, save the file
- Out.write (b);
- //4th step: Turn off the output stream
- Out.close ();
- }
- }</span>
FileReader
- <span style="Font-family:verdana;" >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
- ///1th step: Use the file class to locate a document that declares a Files object
- File F = new file ("D:" + File.separator + "test.txt");
- //2nd step: Instantiating a parent class object from a subclass
- Reader reader = null;
- //Prepare an input object for instantiation by object polymorphism
- reader = new FileReader (f);
- //3rd step: Read, all the contents are read in this array
- char c[] = new char[1024];
- int len = Reader.read (c);
- //4th step: Turn off the input stream
- Reader.close ();
- //Change char array to string output
- System.out.println ("content:" + new String (c, 0, Len));
- }
- }</span>
FileWriter
- <span style="Font-family:verdana;" >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
- ///1th step: Use the file class to locate a document that declares a Files object
- File F = new file ("C:" + File.separator + "test.txt");
- //2nd step: Instantiating a parent class object from a subclass
- Writer out = null;
- //Prepare an Output object, and instantiate it through the polymorphism of the object
- out = new FileWriter (f);
- //3rd step: Write, prepare a string
- String str = "Hello world!!!";
- Out.write (str);
- Out.flush ();
- //4th step: Turn off the output stream
- Out.close ();
- }
- }</span>
Java IO operations: Fileinputstream,fileoutputstream,filereader,filewriter instances