JAVA learning notes ()-byte Transfer to the slave stream, java learning notes

Source: Internet
Author: User

JAVA learning notes ()-byte Transfer to the slave stream, java learning notes
Convert byte streams into bytes streams

Import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. outputStreamWriter;/** InputStreamReader and OutputStreamWriter Classes * are used to convert byte streams into bytes streams. ** note: java does not provide a method to convert a sequence stream to a byte stream */public class Test04 {public static void main (String [] args) throws IOException {test2 ();} // rename the file The input stream is converted to the public static void test1 () throws IOException {// defines the file byte input stream InputStream is = new FileInputStream ("D: \ Java \ hello.txt "); // convert it to InputStreamReader isr = new InputStreamReader (is); int data; while (data = isr. read ())! =-1) {System. out. println (char) data);} isr. close (); is. close () ;}// convert the file byte output stream to the writable stream public static void test2 () throws IOException {// define the file byte output stream OutputStream OS = new FileOutputStream ("D: \ Java \ hello.txt "); // converts it to a bytes stream, specifying the encoding used for writing OutputStreamWriter osw = new OutputStreamWriter (OS," gbk "); // convert the standard byte input stream to the bytes stream, specifying the encoding used for reading InputStreamReader isr = new InputStreamReader (System. in, "gbk"); int data; Syst Em. out. println ("Enter data:"); while (data = isr. read ())! = 'Q') {osw. write (data); osw. flush ();} isr. close (); OS. close ();}}
Adds a buffer to the character input/output stream.
Import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. io. inputStreamReader;/** BufferedReader and BufferedWriter buffer stream * adds a buffer for the character input/output stream */public class Test05 {public static void main (String [] args) throws IOException {test2 ();} // Add a buffer for the character input stream public static void test1 () throws IOException {// convert the standard byte input stream to writable stream I NputStreamReader isr = new InputStreamReader (System. in, "gbk"); // Add BufferedReader buffer to the character input stream br = new BufferedReader (isr); System. out. println ("Enter data:"); String str = null; while (str = br. readLine ())! = Null) {System. out. println (str);} br. close (); isr. close () ;}// use the buffer stream to copy the file public static void test2 () throws IOException {// define a file character input stream FileReader fr = new FileReader ("D: \ Java \ hello.txt "); // Add buffer BufferedReader br = new BufferedReader (fr) to the input stream ); // define a file character output stream FileWriter fw = new FileWriter ("D :\\ Java \ world.txt "); // Add buffer BufferedWriter bw = new BufferedWriter (fw) for the output stream; String str = null; while (Str = br. readLine ())! = Null) {bw. write (str);} bw. flush (); System. out. println ("file copied successfully! "); Bw. close (); br. 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.