JAVA--IO, javaio details

Source: Internet
Author: User

JAVA--IO, javaio details

After a day of study, the basic has a certain understanding of JAVA--IO, now I will share with you.

The JAVA. IO package defines multiple stream types (classes or abstract classes) to implement the input and output functions. You can classify them from different perspectives:

Data streams can be divided into input streams and output streams in different directions.

Different data processing units can be divided into byte streams and batch streams.

Different functions can be divided into node streams and processing streams.

It is the classification of the JAVA-io stream. You must ask the input stream and output stream to distinguish them. The input stream and output stream are measured from the program perspective.

If you have less gossip, go straight to the topic:

1. Read the content of a specified file (in byte stream mode)

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 ("read in total:" + num + "Byte");} catch (IOException e) {e. printStackTrace ();}}}

The code is not difficult. If you have any questions, please leave a message.

2. Read the content of a specified file (read as a forward Stream)

Public class Filereader {/** file input stream (writable 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 ("read in total:" + num + "Byte");} catch (IOException e) {e. printStackTrace ();}}}

Here we will show you the difference between byte streams and byte streams. byte streams are read by one character and one character. The byte streams are different. They are read by one character and one character;

3. file output stream (in bytes)

Public class testFileOutputStream {/** 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 \ index. jsp "); // if this file does not exist in the specified path, the system will automatically create a file for us. output = new FileOutputStream (" E: \ JSP \ jsp_demo001 \ index1.jsp "); // if it is set to true, the original file will be copied under the specified file and not deleted. // output = new FileOu TputStream ("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 copied ");}}

Iv. file output stream (in character format)

Public class Filewriter {/** file input stream (writable 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 ("File Path Problems"); System. exit (0) ;}try {for (int I = 0; I <50000; I ++) {output. write (I);} output. close (); System. out. println ("saved");} catch (IOException e) {e. printStackTrace ();}}}

Finally, we will introduce how to read and save video files:

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 this file does not exist in the specified path, the system will automatically create a file output = new FileOutputStream (" E: \ JSP \ jsp_demo001 \ move1.avi ");} catch (FileNotFoundException e) {System. out. print Ln ("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 copied ");}}

I don't like long texts, but I like this kind of complete code. Of course, I have commented on every part of the above. If you have any questions during your study, please leave a message, I will reply to you as soon as possible.

Statement: if any of the above content is inappropriate, please leave a message in time. Thank you.


JAVA-IO

The File class constructor will throw an IOException, so you must handle this exception in the new File.
You can use try... in the code .... Catch (IOException e) to handle or throws up, let the object that calls this method to handle!
 
In the io stream in Java, when reading a file, when the read is complete, no-1 is returned (why not 0? 0 ?)

What are you talking about? I don't know what you said. I have an example to help you ..
Public class Copy {
Public static void main (String arg []) {
FileInputStream FCM = new FileInputStream ("ss. flv") // assume this is a video file.
Byte [] buf = new byte [1, 1024];
Int I =-1;
While (I = Fi. read (buf )! =-1 ){
FS. read ();

}
System. out. priontln ("file copied ");
FCM. 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.