Java Learning Note (33)-byte stream converted to character stream

Source: Internet
Author: User

Convert a byte stream to a character stream
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;/* * InputStreamReader and OutputStreamWriter class * used to convert a byte stream to a character stream * Note: There is no way to convert a stream of characters to a byte stream in Java */ Public  class Test04 {     Public Static void Main(string[] args)throwsIOException {test2 (); }//Convert file byte input stream to character stream     Public Static void test1()throwsIOException {//define file byte input streamInputStream is =NewFileInputStream ("D:\\java\\hello.txt");//convert it to a character streamInputStreamReader ISR =NewInputStreamReader (IS);intData while(data = Isr.read ())! =-1) {System.out.println (Char) data);        } isr.close ();    Is.close (); }//Convert file byte output stream to character stream     Public Static void test2()throwsIOException {//define file byte output streamOutputStream OS =NewFileOutputStream ("D:\\java\\hello.txt");//convert it to a character stream, specifying the encoding to use when writingOutputStreamWriter OSW =NewOutputStreamWriter (OS,"GBK");//Converts the standard byte input stream to a character stream, specifying the encoding to use when readingInputStreamReader ISR =NewInputStreamReader (System.in,"GBK");intData System.out.println ("Please enter data:"); while((Data=isr.read ())! =' Q ') {osw.write (data);        Osw.flush ();        } isr.close ();    Os.close (); }}
Adding buffering for character input and output streams
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException;ImportJava.io.InputStreamReader;/* * BufferedReader and BufferedWriter Buffer stream * Add buffer for character input/output stream */ Public  class Test05 {     Public Static void Main(string[] args)throwsIOException {test2 (); }//Add buffering for character input stream     Public Static void test1()throwsIOException {//Convert standard byte input stream to character streamInputStreamReader ISR =NewInputStreamReader (System.in,"GBK");//Add buffering for character input streamBufferedReader br =NewBufferedReader (ISR); System.out.println ("Please enter data:"); String str =NULL; while(str = br.readline ())! =NULL) {System.out.println (str);        } br.close ();    Isr.close (); }//Using buffered streams to copy files     Public Static void test2()throwsIOException {//Define a file character input streamFileReader FR =NewFileReader ("D:\\java\\hello.txt");//Add buffering for input streamBufferedReader br =NewBufferedReader (FR);//Define a file character output streamFileWriter FW =NewFileWriter ("D:\\java\\world.txt");//Add buffering for the output streamBufferedWriter BW =NewBufferedWriter (FW); String str =NULL; while(str = br.readline ())! =NULL) {bw.write (str);        } bw.flush (); System.out.println ("File copy succeeded!" ");        Bw.close ();    Br.close (); }}

Java Learning Note (33)-byte stream converted to character stream

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.