I/O streams in Java, java characters

Source: Internet
Author: User

I/O streams in Java, java characters

Character-based reading/writing

Core class of the compaction stream in I/O

The Reader class and Writer class are the parent classes of all the livestream classes and are also abstract classes. FileReader and FileWriter are their subclasses respectively.

Core methods:

Reader:

int read(char [] c, int off, int len);

Writer:

void write(char [] c, int off, int len);
import java.io.*;public class Test{public static void main(String args[]){FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("F:/Android/Java4Android/33/src/a.txt");fw = new FileWriter("F:/Android/Java4Android/33/src/b.txt");char [] c = new char [100];int cLen = fr.read(c,0,c.length);fw.write(c,0,cLen);}catch(Exception e){System.out.println(e);}finally{try{fr.close();fw.close();}catch(Exception e){System.out.println(e);}}}}

for(int i = 0;i <c.length;i++){System.out.println(c[i]);}

 


1 what is the difference between the forward stream and the byte stream in Java I/O?

The byte stream itself does not use a buffer (memory) during operations, but directly operates on the file itself. The swap stream uses a buffer during operations and operates on the file through the buffer.
Two examples can be used to describe:
1: use byte stream not to close the execution

Public static void main (String [] args) throws Exception {// thrown Exception, not processed
// Step 2: use the File class to find an object
File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
// Step 2: instantiate the parent class object through subclass
OutputStream out = null; // prepare an output object.
Out = new FileOutputStream (f); // instantiated by object Polymorphism
// Step 4: Perform the write operation
String str = "Hello World !!! "; // Prepare a string
Byte B [] = str. getBytes (); // string to byte array
Out. write (B); // output the content
// Step 4: Close the output stream
// Out. close (); // not closed
} // You will see the generated file contains "Hello World !!! "

2: Do not close the execution when using the livestream
Public static void main (String [] args) throws Exception {// thrown Exception, not processed
// Step 2: use the File class to find an object
File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
// Step 2: instantiate the parent class object through subclass
Writer out = null; // prepare an output object.
Out = new FileWriter (f); // instantiate through object Polymorphism
// Step 4: Perform the write operation
String str = "Hello World !!! "; // A string
Out. write (str); // output the content
// Step 4: Close the output stream
// Out... the remaining full text>
 
Why stream is used in java? I have read a lot about the concept of stream, but I just don't understand its advantages. I Can't directly transfer strings or shapings.

Because the reading of files or reading from pipelines is actually reading bytes, we need to use byte streams.
However, byte streams are not easy to use in java development, so there is a byte buffer stream that reads multiple bytes for processing. For example, a Chinese character may contain two or three bytes. You need to check the character set used. GBK or UTF8.
Then we need to convert the stream from byte to character.
This process is generally called upward packaging.
A Conversion stream is written out of the byte stream of the file, and a buffer stream is encapsulated. the string is read and can be processed.
Of course, object streams can also be used...
Write files in reverse order.
We recommend that you take a good look at the java API of I/O.

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.