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 ();}}