Java IO Learning (vi) FileInputStream and FileOutputStream

Source: Internet
Author: User
Tags constructor final

This chapter describes FileInputStream and FileOutputStream

Introduction to FileInputStream and FileOutputStream

FileInputStream is the file input stream, which inherits from InputStream.

Typically, we use FileInputStream to get input bytes from a file.

FileOutputStream is the file output stream, which inherits from OutputStream.

Typically, we use FileOutputStream to write data to a File or filedescriptor output stream.

For a description of file, refer to the article "Summary of Java IO series 08".

For the introduction of FileDescriptor, you can refer to the article "Java IO Series 09 FileDescriptor summary."

FileInputStream function Interface

FileInputStream (file file)         //constructor 1: Create "file input stream"
FileInputStream (filedescriptor FD)//constructor 2: Create " File descriptor "file input stream"
FileInputStream (String path)       //Constructor 3: Create "file (path to path)" corresponding "File input stream"
     
int      available ()             //Returns "number of bytes left to read" or "Skip bytes"
void Close     ()                 //off "file input stream"
FileChannel      Getchannel ()    //Return to "FileChannel"
final filedescriptor     getfd ()//return "file descriptor"
int      read ()                  //back to "file input stream" The next byte
int      read (byte[] buffer, int byteoffset, int byteCount)//reads "File input stream" data and exists to the buffer, starting from the Byteoffset storage, Storage length is bytecount.
Long     Skip (long ByteCount)    //Skip ByteCount bytes

FileOutputStream function Interface

FileOutputStream (file file)                   //constructor 1: Create a "file input stream" corresponding to the "Files object"; The default append mode is False, that is, "stream content written to output" is not added to the file in an append manner.
fileoutputstream (File File, Boolean append)   //Constructor 2: Create the file input stream corresponding to the "Files object", and specify "Append mode".
FileOutputStream (filedescriptor fd)           //Constructor 3: Creates "file input stream" corresponding to "file descriptor"; The default append mode is False, that is, "stream content written to output" is not added to the file as an append.
FileOutputStream (String path)                 //Constructor 4: Create file input stream corresponding to file (path to path); The default append mode is False, that is, "stream content written to output" is not added to the file as an append.
FileOutputStream (String path, Boolean append)//Constructor 5: Create the file input stream corresponding to file (path to path), and specify append mode.
     
void                    Close ()      //Turn off "output stream"
FileChannel             Getchannel ()//Back to "FileChannel"
final FileDescriptor    getfd ()      //return "file descriptor"
void                    write (byte[] buffer, int byteoffset, int byteCount)// Writes the buffer to the "file output stream", writes from the byteoffset of the buffer, writes the length is bytecount.
void                    Write (int onebyte)  //write bytes onebyte to file output stream

Sample Programs

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

For API usage for FileInputStream and FileOutputStream, refer to Sample code (Filestreamtest.java):

Import Java.io.File;
Import Java.io.FileDescriptor;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
import Java.io.PrintStream;;
     
Import java.io.IOException; /** * FileInputStream and FileOutputStream Test program * * @author Skywang/public class Filestreamtest {private St
     
    atic final String FileName = "file.txt";
        public static void Main (string[] args) {testwrite ();
    Testread (); /** * FileOutputStream Demo function * Run Result: * In the source directory generated file "File.txt", the file content is "Abcdefghijklmnopqrstuv
     wxyz0123456789 "* * Join, we will fileoutputstream FileOut2 = new FileOutputStream (file, true);
     * Modify to FileOutputStream fileOut2 = new FileOutputStream (file, false);
     * Then execute the program, "file.txt" content becomes "0123456789".
     * The reason is: * () FileOutputStream fileOut2 = new FileOutputStream (file, true); * It writes content to the file in "Append mode".
     The content that is written, appended to the original content. *(fileoutputstream) FileOut2 = new FileOutputStream (file, false); * It writes content to the file in "new mode."
     That is, delete the original contents of the file, and then write again. /private static void Testwrite () {try {//Create file "file.txt" corresponding to file Object File File = new
            File (FileName);
            Creates a FileOutputStream object corresponding to the file "file.txt" by default, by turning off append mode fileoutputstream fileOut1 = new FileOutputStream (file); Create FileOutputStream corresponding PrintStream for easy operation.
            The PrintStream write interface is more convenient for printstream out1 = new PrintStream (FILEOUT1);
            Write 26-Letter out1.print ("abcdefghijklmnopqrstuvwxyz") to "file";
     
            Out1.close ();
            Create the FileOutputStream object corresponding to the file "file.txt" and open the Append mode fileoutputstream fileOut2 = new FileOutputStream (file, true); Create FileOutputStream corresponding PrintStream for easy operation.
            The PrintStream write interface is more convenient for printstream out2 = new PrintStream (FILEOUT2);
            Write "0123456789" + newline character out2.println ("0123456789") to "file"; Out2.Close ();
        catch (IOException e) {e.printstacktrace (); }/** * FileInputStream demo/private static void Testread () {try {/
            /Method 1: New FileInputStream object//New file "file.txt" corresponds to file Object file = new filename;
     
            FileInputStream in1 = new FileInputStream (file);
     
            Method 2: New FileInputStream object FileInputStream in2 = new FileInputStream (FileName);
            Method 3: Create a new FileInputStream object//Get File "file.txt" corresponding "file descriptor" FileDescriptor Fdin = In2.getfd ();
     
            Create a "FileInputStream" object based on the "file descriptor" FileInputStream in3 = new FileInputStream (Fdin);
            Test read (), which reads a byte char c1 = (char) in1.read ();
     
            System.out.println ("c1=" +c1);
     
            Test Skip (Long ByteCount), skipping 4 bytes in1.skip (25); Test read (byte[] buffer, int byteoffset, int byteCount)
            byte[] buf = new BYTE[10];
            In1.read (buf, 0, buf.length);
     
     
            System.out.println ("buf=" + (new String (BUF));
            Creates a bufferedinputstream bufferedinputstream corresponding to the "FileInputStream" Object bufin = new Bufferedinputstream (in3);
            Reads a byte char c2 = (char) bufin.read ();
     
            System.out.println ("c2=" +c2);
            In1.close ();
            In2.close ();
        In3.close ();
        catch (IOException e) {e.printstacktrace (); }
    }
}

Run Result:

C1=a

buf=0123456789

C2=a

Results show:

Run the program, will be in the source location of the new file "file.txt." Its content is "abcdefghijklmnopqrstuvwxyz0123456789".

Source: http://www.cnblogs.com/skywang12345/p/io_07.html

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.