Java Io stream, reading and writing files in batches

Source: Internet
Author: User

Package COM. hoo. utils; </P> <p> Import Java. io. bufferedreader; <br/> Import Java. io. bufferedwriter; <br/> Import Java. io. file; <br/> Import Java. io. filereader; <br/> Import Java. io. filewriter; <br/> Import Java. io. ioexception; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> public class operatefileutils {</P> <p> /********************* **************************************** ************** <Br/> * <B> function: </B> in Java, replace the path with/<br/> * @ createdate 11:18:45 <br/> * @ Param file to replace the file <br/> * @ return: path after replacement <br/> */<br/> Private Static string getavailablepath (File file) {<br/> return file. getpath (). replace ('//','/'); <br/>}</P> <p> /*************************** **************************************** * ******* <br/> * <B> function: </B> return all objects in a folder <br/> * @ createdate 2010-6 -4 11:21:07 am <br/> * @ Param filelist: file set found <br/> * @ Param path <br/> * @ Param filesuffix: file suffix to be found <br /> */<br/> Public static void getfiles (list <File> filelist, string path, string filesuffix) {<br/> file = new file (PATH); <br/> file [] files = file. listfiles (); <br/> If (Files = NULL) {<br/> return; <br/>} else {<br/> for (INT I = 0; I <files. length; I ++) {<br/> // determines whether it is a folder <br/> If (File S [I]. isdirectory () {<br/> // call the getfiles method recursively to obtain all files <br/> getfiles (filelist, getavailablepath (files [I]), filesuffix ); <br/>} else if (files [I]. getname (). lastindexof (filesuffix )! =-1) {// only process files suffixed with filesuffix <br/> // copyfileandaddpackagename (files [I]); <br/> filelist. add (files [I]); // Add to file set <br/>}< br/> // system. out. println (files [I]. getabsolutepath (). replace ('//', <br/> //'/')); <br/>}</P> <p> /***************** **************************************** * ***************** <br/> * <B> function: </B> copy the file content to the specified file <br/> * @ createdate 12:09:42 <br/> * @ autho R 3 hhuj Hu Jia <br/> * @ Param file the file to be operated on <br/> * @ Param tofile the file to be saved <br/> */<br/> Public static void copyfile2tempfile (list <File> filelist, file tofile) {<br/> bufferedwriter BW = NULL; <br/> bufferedreader BR = NULL; <br/> filewriter = NULL; <br/> filereader = NULL; <br/> try {<br/> stringbuilder sb = new stringbuilder (); <br/> string content = ""; <br/> for (File textfile: filelis T) {<br/> system. out. println (textfile. getname (); <br/> filereader = new filereader (textfile); // read the file content <br/> Br = new bufferedreader (filereader ); <br/> while (content = BR. readline ())! = NULL) {<br/> system. Out. println (content); <br/> If (! Content. trim (). equals ("") {<br/> Sb. append (content );//. append ("/R"); <br/>}< br/> filewriter = new filewriter (tofile ); // write the content in the file you just read to the file <br/> BW = new bufferedwriter (filewriter); <br/> BW. write (sb. tostring (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> try {<br/> BW. close (); <br/> Br. close (); <br/> filewriter. close (); <br/> filereader. close (); <br/>} Ca Tch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> /***************** **************************************** * ***************** <br/> * <B> function: </B> read a single file <br/> * @ createdate 2010-6-4 01:42:33 <br/> * @ Param file the file that reads the content <br/> * @ Param tofile file <br/> */<br/> Public static void copyfile2tempfile (File file, file tofile) {<br/> bufferedwriter BW = NULL; <br/> Buffer Edreader BR = NULL; <br/> filewriter = NULL; <br/> filereader = NULL; <br/> try {<br/> stringbuilder sb = new stringbuilder (); <br/> string content = ""; <br/> filereader = new filereader (File ); // read the file content <br/> Br = new bufferedreader (filereader); <br/> while (content = BR. readline ())! = NULL) {<br/> system. Out. println (content); <br/> If (! Content. trim (). equals ("") {<br/> Sb. append (content );//. append ("/R"); <br/>}< br/> filewriter = new filewriter (tofile ); // write the content in the file you just read to the file <br/> BW = new bufferedwriter (filewriter); <br/> BW. write (sb. tostring (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> try {<br/> BW. close (); <br/> Br. close (); <br/> filewriter. close (); <br/> filereader. close (); <br/>} catch (IO Exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> List <File> filelist = new arraylist <File> (); <br/> getfiles (filelist, "C:/txt_test ",". TXT "); <br/> getfiles (filelist," C:/TXT ",". TXT "); <br/> // put the read content into tempfile <br/> file tempfile = new file (" C: // temp.txt "); <br/> If (! Tempfile. exists () {<br/> try {<br/> // create a <br/> tempfile = file. createtempfile ("Temp ",". TXT ", new file (" C: // "); <br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> // copyfile2tempfile (filelist, tempfile); <br/> copyfile2tempfile (new file ("C: // a.txt "), tempfile); <br/>}< br/>}

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.