Use byte stream and character stream respectively to complete the following programs:
1. Create a new. txt file "Test.txt" under the specified path and use the program to write the following in the file:
"Java is an object-oriented programming language that can compose cross-platform applications, and is made by Sun Microsystems.
Introduced in May 1995, the Java programming language and Java platform (ie, javase, Java EE, javame) collectively. Java Technology has
Superior versatility, efficiency, platform portability, and security are widely used in personal PCs, data centers, game consoles, science super
Computers, mobile phones and the Internet, as well as the world's largest developer professional community. In the global cloud computing and mobile internet industry
Environment, Java has a significant advantage and broad prospects. "
2. Use the program to read the contents of the Test.txt file and print it in the console
3. Use the program to copy Test.txt to Test1.txt
Packageday041301;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;Importorg.junit.Test; Public classHomework {//Mr.song@Test Public voidtest2 () {//FileOutputStream fos = new FileOutputStream (New File ("test.txt")); //bufferedoutputstream bos = new Bufferedoutputstream (FOS); Try{bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (NewFile ("Test.txt"))); String C= "Java is an object-oriented programming language that can compose cross-platform applications, and is the Java programming language and Java platform (i.e. Javase, Ja) introduced by Sun Microsystems Company in the" + "May 1995. Vaee, Javame) collectively. Java technology with \ r \ n "+" excellent versatility, efficiency, platform portability and security, widely used in personal PC, data center, game console, science super \ r \ n "+" computer, mobile phone and internet The world's largest developer professional community. In the global cloud computing and mobile internet industry \ r \ n Environment, Java has a significant advantage and broad prospects. "; Bos.write (C.getbytes ()); Bos.flush (); Bos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //my maked@Test//file copy. Using character streams Public voidTest6 () {bufferedreader br=NULL; BufferedWriter BW=NULL; Try{br=NewBufferedReader (NewFileReader ("Test.txt")); Bw=NewBufferedWriter (NewFileWriter ("Test001.txt")); String str; while(str = br.readline ())! =NULL) {bw.write (str); } }Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally { if(BW! =NULL) { Try{bw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(BR! =NULL) { Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }} @Test//read test.txt with a byte stream Public voidTest5 () {Bufferedinputstream bis=NULL; Try{bis=NewBufferedinputstream (NewFileInputStream (NewFile ("Test.txt"))); byte[] B =New byte[10000];//if here byte[] number leader through small, may not be able to store some Chinese characters. intLen; while(len = Bis.read (b))! =-1) {String str=NewString (b,0, Len); System.out.print (str); } }Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ if(bis! =NULL) { Try{bis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }} @Test Public voidtest4 () {bufferedreader br=NULL; Try{br=NewBufferedReader (NewFileReader ("Test1.txt")); intLen; Char[] C =New Char[40]; while(len = Br.read (c))! =-1) {String str=NewString (c, 0, Len); System.out.print (str); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally { if(BR! =NULL) { Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }} @Test Public voidTest3 () {//character Stream Try{bufferedwriter bw=NewBufferedWriter (NewFileWriter ("Test1.txt")); String C= "Java is an object-oriented programming language that can compose cross-platform applications, and is the Java programming language and Java platform (i.e. Javase, Ja) introduced by Sun Microsystems Company in the" + "May 1995. Vaee, Javame) collectively. Java technology with \ r \ n "+" excellent versatility, efficiency, platform portability and security, widely used in personal PC, data center, game console, science super \ r \ n "+" computer, mobile phone and internet The world's largest developer professional community. In the global cloud computing and mobile internet industry \ r \ n Environment, Java has a significant advantage and broad prospects. "; Bw.write (c); Bw.flush (); Bw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Test Public voidtest1 () {FileOutputStream fos=NULL;//Byte stream Try{fos=NewFileOutputStream (NewFile ("Test.txt")); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } bufferedoutputstream Bos=NewBufferedoutputstream (FOS); String C= "Java is an object-oriented programming language that can compose cross-platform applications, and is the Java programming language and Java platform (ie, javase) introduced by Sun Microsystems Company in \ r \ n" + "in May 1995. , Javame) collectively. Java technology has \ r \ n "+" excellent versatility, efficiency, platform portability and security, widely used in personal PCs, data centers, game consoles, science super \ r \ n "+" computers, mobile phones and the Internet, while the global The largest developer professional community. In the global cloud computing and mobile internet industry \ r \ n Environment, Java has a significant advantage and broad prospects. "; Try{bos.write (c.getbytes ()); Bos.flush (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } if(Bos! =NULL) { Try{bos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Java Learning Diary-----IO Stream exercise