Java IO stream-related knowledge points and javaio stream knowledge points

Source: Internet
Author: User

Java IO stream-related knowledge points and javaio stream knowledge points

(1) write operations using outputStream byte output stream below

Package zdbIO;

Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. OutputStream;

Public class OutputStreamDemo1 {

/**
* @ Throws IOException
* @ Throws IOException
*
*/
Public static void main (String [] args) throws IOException {

/**
* Steps for using an IO stream:
* 1. Use file to find the file to be operated
* 2. (The inputStream, outStream, reader, and writer are instantiated using byte streams or the subclass of bytes streams)
* 3. Perform read/write operations
* 4. Disable the stream, except BufferedReader.
*/

File file = new File ("f:" + File. separator + "zdb1.txt"); // use file to locate the File to be operated
OutputStream out = null;
Out = new FileOutputStream (file, true); // use the subclass of OutputStream for instantiation
String str = "XXX's ten-year life plan must have a plan so that your life will have a clear direction and will not be lost. "; // Information to be output
Byte B [] = str. getBytes (); // Changes str to a byte array
Out. write (B); // write data
Out. close (); // close the stream

}

}

(2) The first method for reading an inputStream byte stream below:


Package zdbIO;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. InputStream;

/**
* Read operations through inputStream byte streams
*
*/
Public class InputStreamDemo {
Public static void main (String [] args) throws Exception {
// Note that this file must exist; otherwise, a java. io. FileNotFoundException exception occurs.
// File. separator indicates the delimiter, Which is \ in Windows and/in unix. This allows cross-platform
File file = new File ("f:" + File. separator + "zdb1.txt ");
InputStream input = null;
Input = new FileInputStream (file );
Byte B [] = new byte [1024]; // open up a piece of memory to store the read content
Int len = input. read (B); // read the object to the character array.
// New String (byte [] bytes, int offset, int length ),
// Indicates that a string will be created, starting from offset and length
System. out. println (new String (B, 0, len ));
Input. close ();

}

}


/**
* In this method, the size of the opened space is limited. Therefore, the size of the opened space can be determined by the file size,
* You can use byte B [] = new byte [(int) file. length ()] to create the opened space,
* You can then read data one by one using the read () method.
*
*/

(2) The second method of using inputStream byte stream for read operations below

Package zdbIO;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import java. io. InputStream;

Public class InputStreamDemo1 {
Public static void main (String [] args) throws Exception {
File file = new File ("f:" + File. separator + "zdb1.txt ");
InputStream input = null;
Input = new FileInputStream (file );
Byte B [] = new byte [(int) file. length ()];
For (int I = 0; I <B. length; I ++ ){
B [I] = (byte) input. read ();
}
System. out. println (new String (B ));
Input. close ();
}

}


(3) The first method for reading data using the Reader byte stream below:
Package zdbIO;

Import java. io. File;

Import java. io. FileReader;
Import java. io. Reader;

Public class ReaderDemo {
Public static void main (String [] args) throws Exception {
File file = new File ("f:" + File. separator + "zdb1.txt ");
Reader in = null;
In = new FileReader (file );
Char c [] = new char [1024];
Int len = in. read (c );
System. out. println (new String (c, 0, len ));
In. close ();
}

}

(3) The second method of using Reader byte stream for read operations below:
Package zdbIO;

Import java. io. File;

Import java. io. FileReader;
Import java. io. Reader;

Public class ReaderDemo {
Public static void main (String [] args) throws Exception {
File file = new File ("f:" + File. separator + "zdb1.txt ");
Reader in = null;
In = new FileReader (file );
Char c [] = new char [1024];
Int len = in. read (c );
System. out. println (new String (c, 0, len ));
In. close ();
}

}
(4) write operations using the Writer byte stream below:
Package zdbIO;

Import java. io. File;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. Writer;

Public class WriterDemo {
/**
* Write operations can be performed using the volume stream writer.
* @ Throws IOException
*
*/

Public static void main (String [] args) throws IOException {
File file = new File ("f:" + File. separator + "zdb1.txt ");
Writer writer = null;
Writer = new FileWriter (file, true );
String str = "have you studied well today? Have you made progress today? Learn to think twice a day ";
Writer. write (str );
Writer. close ();

}

}
(5) The difference between two types of traffic:
1. In the write operation Writer in the livestream, you must disable or refresh the stream after the operation is complete. Otherwise, the write operation fails.
2. byte streams directly interact with files without caching. The Bytes stream interacts with files through caching;
3. The content stored on the transmission and hard disk is in the byte format, so there are many operations in the byte format, while there are a lot of streams in the file operation;
















































 

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.