The basic use of FileInputStream and FileOutputStream in Java __java

Source: Internet
Author: User
Tags int size
what is InputStream and OutputStream.

InputStream and OutputStream are abstract classes that are the parent class for all byte input and output streams. Here, we first need to clarify two concepts: InputStream (Input stream): the input stream is used to read data. --> > > Read outputstream (Output stream): Output stream is used to write data. --> > > Write file input stream--fileinputstream

FileInputStream gets input bytes from a file in the file system. Construction Method

Creates a fileinputstream by opening a connection to the actual file, which is specified by file object filename in the filesystem. Public
fileinputstream (file file);

Creates a fileinputstream by opening a connection to the actual file, which is specified by the pathname name in the file system. Public
FileInputStream (String name);
the usual method reads a byte-size data from the input stream
Reads a byte of data from this input stream. public
int read ();
read one byte array at a time from the input stream
Reads up to b.length bytes of data from this input stream into a byte array. public
int Read (byte[] b);

Reads up to Len bytes of data from this input stream into a byte array. OFF: The starting offset in the destination array B. public
int Read (byte[] b,int off,int len);
To read data from a file:
Import Java.io.FileInputStream;

/**
 * FileInputStream: node stream (low-level stream), read data from file
 * @author Administrator */
 public
class FISDemo01 { Public
    static void Main (string[] args) {
        String content=null;
        try {
            int size=0;
            Defines a byte buffer, the size of which is defined as required
            byte[] buffer=new byte[1024];
            FileInputStream fis=new FileInputStream ("FOSDemo.txt");
            Loop to read the data in that file while
            ((size=fis.read (buffer)!=-1) {
                content=new String (buffer, 0, size);
                SYSTEM.OUT.PRINTLN (content);
            }
        Closes this file input stream and releases all system resources associated with this stream. 
        fis.close ();
        } catch (Exception e) {
            e.printstacktrace ();}}}
file output stream--fileoutputstream

The file output stream is used to write data to a file. Construction Method

Creates a file output stream that writes data to the file represented by the specified file object. Public
fileoutputstream (file file);

Creates a file output stream that writes data to the file represented by the specified file object. If the second argument is true, the byte is written to the end of the file rather than to the beginning of the file. Public
FileOutputStream (File file,boolean append);

Creates an output file stream that writes data to a file with the specified name. Public
FileOutputStream (String name);

Creates an output file stream that writes data to a file that has the specified name. If the second argument is true, the byte is written to the end of the file rather than to the beginning of the file. Public
FileOutputStream (String name,boolean append);
the usual method writes a byte-size data to a file
Writes a byte-size data to the file public
void write (int b);
writes one byte array of data to a file at a one-time
Writes B.length bytes from the specified byte array to this file output stream. Public
void Write (byte[] b);

Writes Len bytes from the offset off at the byte array to this file output stream. Public 
void Write (byte[] b,int off,int len);
write data to a file:
Import Java.io.FileOutputStream;

/**
 * FileOutputStream: node stream (low-level stream), write data to file 
 * @author Administrator */Public
class FOSDemo01 { Public
    static void Main (string[] args) {
        try {
            //write byte array to file
            String "output stream is used to write data.) ";
            FileOutputStream fos = new FileOutputStream ("FOSDemo.txt");
            Fos.write (Font.getbytes ());
            Closes this file output stream and releases all system resources associated with this stream. This file output stream can no longer be used for write bytes. If the stream has a channel associated with it, the channel is closed. 
            fos.close ();
        } catch (Exception e) {
            e.printstacktrace ();}}}
implementing replication of files using FileInputStream and FileOutputStream
Import Java.io.FileInputStream;

Import Java.io.FileOutputStream; /** * Replication of files using file input streams and file output streams * @author Administrator * * */public class Summaryfisandfos {public static void main (St
         Ring[] args) {/** * 1. Read the contents of the file into the input stream * 2. Writes the data in the input stream through the output stream to the destination file * 3. Close the input and output streams
            */try {long begin=system.currenttimemillis ();
            Reads data from the input stream FileInputStream fis=new fileinputstream ("FOSDemo.txt");
            Writes data to the output stream FileOutputStream fos=new fileoutputstream ("FISAndFOSDest.txt");
            First define a byte buffer, reduce I/o times, improve read and write efficiency byte[] buffer=new byte[10240];
            int size=0;
            while (size=fis.read (buffer)!=-1) {fos.write (buffer, 0, size);
            } fis.close ();
            Fos.close ();
            Long End=system.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Use file input stream and file output stream to complete copying of files.")
        Time consuming: "+ (End-begin) +" millisecond ");
       catch (Exception e) {     E.printstacktrace ();
    }//Resolve JNI problem (Java Native Interface) system.exit (0); }
}

Run Result:
Use the file input stream and file output stream to complete the copy of the file. Time consuming: 17 milliseconds

The above is basically we often use the method of introduction, remember finally must close () Oh. The above content only represents my personal point of view, if there is anything wrong, please correct the way. Reprint please indicate the source. Thank you.

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.