Java IO operations: Fileinputstream,fileoutputstream,filereader,filewriter instances

Source: Internet
Author: User
Tags array to string

FileInputStream

 
  1. <span style="Font-family:verdana;"  >import java.io.File;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.InputStream;
  4. Public class Testfileinputstream {
  5. public static void Main (string[] args) throws Exception { //exception thrown, not processed
  6. //1th step: Use the file class to find a document
  7. File F = new file ("C:" + File.separator + "test.txt"); Declaring a File object
  8. //2nd step: Instantiating a parent class object from a subclass
  9. InputStream input = null;
  10. //Prepare an input object for instantiation by object polymorphism
  11. input = new FileInputStream (f);
  12. //3rd step: Read, all the contents are read in this array
  13. byte b[] = new byte[1024];
  14. int len = Input.read (b);
  15. //4th step: Turn off the input stream
  16. Input.close ();
  17. //change byte array to string output
  18. System.out.println ("Length of the read-in data:" + len);
  19. System.out.println ("content:" + new String (b, 0, Len));
  20. }
  21. }</span>

FileOutputStream

 
  1. <span style="Font-family:verdana;"  >import java.io.File;
  2. Import Java.io.FileOutputStream;
  3. Import Java.io.OutputStream;
  4. Public class Testfileoutputstream {
  5. public static void Main (string[] args) throws Exception { //exception thrown, not processed
  6. //1th step: Use the file class to find a document
  7. File F = new file ("C:" + File.separator + "test.txt"); //Declare file object
  8. //2nd step: Instantiating a parent class object from a subclass
  9. OutputStream out = null;
  10. //Prepare an Output object, and instantiate it through the polymorphism of the object
  11. out = new FileOutputStream (f);
  12. //3rd step: Write, prepare a string
  13. String str = "Hello world!!!";
  14. //can only output a byte array, so the string becomes a byte array
  15. byte b[] = Str.getbytes ();
  16. //Output The content, save the file
  17. Out.write (b);
  18. //4th step: Turn off the output stream
  19. Out.close ();
  20. }
  21. }</span>

FileReader

 
  1. <span style="Font-family:verdana;"  >import java.io.File;
  2. Import Java.io.FileReader;
  3. Import Java.io.Reader;
  4. Public class Testfilereader {
  5. public static void Main (string[] args) throws Exception { //exception thrown, not processed
  6. ///1th step: Use the file class to locate a document that declares a Files object
  7. File F = new file ("D:" + File.separator + "test.txt");
  8. //2nd step: Instantiating a parent class object from a subclass
  9. Reader reader = null;
  10. //Prepare an input object for instantiation by object polymorphism
  11. reader = new FileReader (f);
  12. //3rd step: Read, all the contents are read in this array
  13. char c[] = new char[1024];
  14. int len = Reader.read (c);
  15. //4th step: Turn off the input stream
  16. Reader.close ();
  17. //Change char array to string output
  18. System.out.println ("content:" + new String (c, 0, Len));
  19. }
  20. }</span>

FileWriter

 
    1. <span style="Font-family:verdana;"  >import java.io.File;
    2. Import Java.io.FileWriter;
    3. Import Java.io.Writer;
    4. Public class Testfilewriter {
    5. public static void Main (string[] args) throws Exception { //exception thrown, not processed
    6. ///1th step: Use the file class to locate a document that declares a Files object
    7. File F = new file ("C:" + File.separator + "test.txt");
    8. //2nd step: Instantiating a parent class object from a subclass
    9. Writer out = null;
    10. //Prepare an Output object, and instantiate it through the polymorphism of the object
    11. out = new FileWriter (f);
    12. //3rd step: Write, prepare a string
    13. String str = "Hello world!!!";
    14. Out.write (str);
    15. Out.flush ();
    16. //4th step: Turn off the output stream
    17. Out.close ();
    18. }
    19. }</span>

Java IO operations: Fileinputstream,fileoutputstream,filereader,filewriter instances

Related Article

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.