I/O Stream copying text

Source: Internet
Author: User
Package IO; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. outputstream Writer; public class Io stream {/*** @ Param ARGs */public static void main (string [] ARGs) {bufferedwr ();} Private Static void show (File file) {file [] filelist = file. listfiles (); For (file F: filelist) {If (F. isfile () {If (F. getname (). endswith (". TXT ") system. out. println (F. getname () ;}else {Show (f) ;}} Private Static void Delete (File file) {file [] filelist = file. listfiles (); For (file F: Filelist) {If (F. isfile () {f. delete () ;}else {Delete (f) ;}} file. delete ();} /*** abstract class byte stream read inputstream byte stream output outputstream ** internal stream read Reader internal stream output writer ** implementation class batearrayoutputstream fileoutputstream * constructor fileoutputstream (File file) fileoutputstream (string path) ** @ throws * // write a string to a file/*** append the file to fileoutputstream Fos = new fileoutputstream ("a.txt", tr UE); newline \ r \ n */public static void strtofile () {// create a byte output stream object string STR = "hello "; try {// file = new file ("D: \ a.txt"); // fileoutputstream FS = new fileoutputstream (File); fileoutputstream Fos = new fileoutputstream ("a.txt ", true); FOS. write ("Hello \ r \ nhello ". getbytes (); FOS. close ();} catch (exception e) {e. printstacktrace () ;}}/*** reads the file content and outputs it to the console */public static void readfromfile () {File = new file ("a.txt"); fileinputstream FCM = NULL; try {FD = new fileinputstream (File); byte [] bys = new byte [1024]; int Len = 0; while (LEN = Fi. read ())! =-1) {system. out. print (char) Len) ;}} catch (exception e) {e. printstacktrace ();} finally {try {FCM. close ();} catch (ioexception e) {e. printstacktrace () ;}}/ *** copy a text file from a.txt to B .txt **/public static void copybyte () {try {fileinputstream is = new fileinputstream ("a.txt "); fileoutputstream OS = new fileoutputstream ("B .txt"); int Len = 0; byte [] bys = new byte [1024]; while (LEN = I S. Read (bys ))! =-1) {OS. write (bys, 0, Len);} OS. close (); is. close ();} catch (exception e) {e. printstacktrace ();} finally {}}/*** bufferedoutputstream buffer output */public static void bufferedoutputdemo () {try {fileoutputstream Fos = new fileoutputstream ("a.txt "); fileinputstream FCM = new fileinputstream ("B .txt"); bufferedoutputstream Bos = new bufferedoutputstream (FOS); bufferedinputstream Bis = New Buffer Edinputstream (FCM); int Len = 0; byte [] bys = new byte [1024]; while (LEN = bis. Read (bys ))! =-1) {Bos. write (bys, 0, Len);} Bos. close (); FOS. close ();} catch (exception e) {e. printstacktrace () ;}}/*** outputstreamwriter (file, character) is the conversion of stream bytes + encoding method ** inputstreamwriter (file, character) is the conversion stream byte + encoding method ** flush () method to refresh the buffer * // *** streams copy file */public static void copyzifuliu () {try {outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream ("a.txt"); inputstreamreader oiw = ne W inputstreamreader (New fileinputstream ("c.txt"); int Len = 0; char [] B = new char [1024]; while (LEN = oiw. read (B ))! =-1) {OSW. write (B, 0, Len); OSW. flush ();} OSW. close (); oiw. close ();} catch (exception e) {e. printstacktrace () ;}/ *** filewriter = fileoutputstream + encoding filereader = fileinputstream + encoding */public static void copyrw () {try {filereader reader = new filereader ("a.txt"); filewriter writer = new filewriter ("d.txt"); int Len = 0; char [] C = new char [1024]; while (LEN = reader. read (c ))! =-1) {writer. write (C, 0, Len); writer. flush ();} writer. close (); reader. close ();} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace () ;}/ *** bufferedwriter (writer out) * bufferedreader (reader read) * special method of Character Buffer stream: * newline (): determined by the system line feed * Readline (): one row of data read */public static void bufferedwr () {try {// bufferedwriter BW = new bufferedwriter (New outputstreamwriter (new F Ileoutputstream ("e.txt"); bufferedwriter BW = new bufferedwriter (New filewriter ("e.txt ")); // bufferedreader BR = new bufferedreader (New inputstreamreader (New fileinputstream ("a.txt"); bufferedreader BR = new bufferedreader (New filereader ("a.txt ")); // int Len = 0; string S = NULL; char [] C = new char [1024]; while (S = (BR. readline ()))! = NULL) {// BW. write (C, 0, Len); // BW. flush (); BW. write (s); BW. newline (); BW. flush ();} BW. close (); BR. close ();} catch (exception e) {e. printstacktrace ();}}}

 

I/O Stream copying text

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.