Java Io operations: fileinputstream, fileoutputstream, filereader, and filewriter instances

Source: Internet
Author: User

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

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.