Java--io

Source: Internet
Author: User

After a day of study, the basic has a certain understanding of Java--io, and now share with you.

Java. The IO package defines the types of multiple streams (classes or abstract classes) to implement input and output functions, which can be categorized from different angles:

Can be divided into input stream and output stream according to different direction of data flow

Can be divided into byte stream and character stream according to the different units of processing data

According to the different functions can be divided into node stream and processing flow

is the classification of the Java--io stream; see, you must be asking what the input and output streams are distinguished by, the input and output streams are measured in terms of the program.

Gossip less talk, now straight to the topic:

Read the contents of the specified file (read by byte stream)

public class Testfileinputstream {    /** file input stream (byte stream input)     * @param args     *    /public static void main (string[] args) {        FileInputStream input = null;        int b = 0;        try {            input = new FileInputStream ("e:\\jsp\\jsp_demo001\\index.jsp");        } catch (FileNotFoundException E) {            SYSTEM.OUT.PRINTLN ("File does not exist");            System.exit (0);        }        Long num = 0;        try {            while ((B=input.read ())!=-1) {                System.out.print ((char) b);                num++;            }            Input.close ();            System.out.println ();            SYSTEM.OUT.PRINTLN ("total read:" +num+ "bytes");        } catch (IOException e) {            e.printstacktrace ();}}    }

Code is not difficult, if you have any questions please leave a message.

Second, read the contents of the specified file (read in a character stream)

public class FileReader {    /** file input stream (character stream input)     * @param args     *    /public static void main (string[] args) {        FileReader input = null;        int b = 0;        try {            input = new FileReader ("e:\\jsp\\jsp_demo001\\index.jsp");        } catch (FileNotFoundException E) {            SYSTEM.OUT.PRINTLN ("File does not exist");            System.exit (0);        }        Long num = 0;        try {            while ((B=input.read ())!=-1) {                System.out.print ((char) b);                num++;            }            Input.close ();            System.out.println ();            SYSTEM.OUT.PRINTLN ("total read:" +num+ "bytes");        } catch (IOException e) {            e.printstacktrace ();}}    }

Here to illustrate a point, the difference between the byte stream and the character stream, the byte stream is a character to read a character, the character stream is different, it is a character to read one character;

Third, the file output stream (in bytes)

 public class Testfileoutputstream {/** file output stream (Byte stream) * @param args */public static void Main (string[] Arg        s) {int b = 0;        FileInputStream input = null;        FileOutputStream output = null;            try {input = new FileInputStream ("e:\\jsp\\jsp_demo001\\index.jsp");            If the file does not exist under the specified path, the system automatically creates a file for us, output = new FileOutputStream ("e:\\jsp\\jsp_demo001\\index1.jsp");        Set to True to copy under the specified file without deleting the original file//output = new FileOutputStream ("e:\\jsp\\jsp_demo001\\index1.jsp", true);            } catch (FileNotFoundException e) {System.out.println ("The specified file does not exist");        System.exit (0);            } try {while ((B=input.read ())!=-1) {output.write ((char) b);            } input.close ();        Output.close ();            } catch (IOException e) {System.out.println ("File copy Error");        System.exit (0);    } System.out.println ("File copy Complete"); }}

Iv. file output stream (in characters)

public class Filewriter {    /** file input stream (character stream output)     * @param args     *    /public static void main (string[] args) {        FileWriter output = null;            try {                output = new FileWriter ("E:\\jsp\\jsp_demo001\\cn.txt");            } catch (IOException E) {                System.out.println ("Problem with file path");                System.exit (0);            }        try {for                (int i=0; i<50000; i++) {                    output.write (i);                }            Output.close ();            System.out.println ("Save Complete");        } catch (IOException e) {            e.printstacktrace ();}}    }

Finally, we introduce a video file read and save:

public class Fileoutputstream_media {    /** file output stream (Byte stream)     * @param args     *    /public static void main (string[] args) {        int b = 0;        FileInputStream input = null;        FileOutputStream output = null;        try {            input = new FileInputStream ("E:\\jsp\\jsp_demo001\\move.avi");            If the file does not exist under the specified path, the system will automatically create a file for us            output = new FileOutputStream ("E:\\jsp\\jsp_demo001\\move1.avi");        } catch (FileNotFoundException e) {            System.out.println ("The specified file does not exist");            System.exit (0);        }        try {            while ((B=input.read ())!=-1) {                output.write ((char) b);            }            Input.close ();            Output.close ();        } catch (IOException e) {            System.out.println ("File copy Error");            System.exit (0);        }        System.out.println ("File copy Complete");}    }

I do not like long-winded text, like this direct complete code, of course, every place above I have made comments, if there is any doubt in your learning process, please leave a message, I will give you an answer as soon as possible.

Java--io

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.