Java read/write files-integrate the functions I need through multiple searches (read and write all the content in the TXT files with the specified name under the specified folder to a specified TXT file)

Source: Internet
Author: User

For example, directly add the Code:

Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstreamwriter;

Public class testfile {
Public static void main (string [] ARGs ){
File F = new file (
"E: // monitor // monitor-motorola_lichengbei // Tuesday January 22, 2013 // motorcycle milestone-2013-1-22 // Baidu // 2012.12.04 _ 19.13.03 ");
Try {
Testfile. filelist (f );
} Catch (exception e ){
}
}

Public static void filelist (File file ){
File [] files = file. listfiles ();
If (files! = NULL ){
For (File F1: Files ){
If (f1.getabsolutepath (). indexof ("(length_le_2 )")! =-1 ){
System. Out. println (f1.getabsolutepath ());
Writer (file, f1.getabsolutepath ());
Reader (f1.getabsolutepath (), file );
}
}
}
}

Public static void reader (string pathname, file files ){
Try {
String encoding = "UTF-8 ";
File file = new file (pathname );
If (file. isfile () & file. exists ()){
Inputstreamreader READ = new inputstreamreader (
New fileinputstream (file), encoding );
Bufferedreader = new bufferedreader (read );
String linetxt = NULL;
While (linetxt = bufferedreader. Readline ())! = NULL ){
System. Out. println (linetxt. tostring (). Trim ());
Writer (files, linetxt );
}
Writer (files, "--------------------------------------------------------------" + "\ r \ n ");
Read. Close ();
} Else {
System. Out. println ("the specified file cannot be found! ");
}
} Catch (exception e ){
System. Out. println ("An error occurred while reading the file content ");
E. printstacktrace ();
}
}
 
Public static void writer (File file, string content ){
Try {
String outfilepath = file + "// length_le_2.txt ";
// Bufferedwriter writer = new bufferedwriter (New filewriter (new file (outfilepath), true ));
// Writer. Write (content + "\ n ");
// Writer. Close ();

Fileoutputstream Fos = new fileoutputstream (outfilepath, true );
Outputstreamwriter OSW = new outputstreamwriter (FOS, "UTF-8 ");
OSW. Write (content + "\ r \ n ");
OSW. Flush ();
OSW. Close ();
} Catch (exception e ){
}
}
 
}

Nothing changed. Copy it directly from eclipse. The function is to put E: // monitor // monitor-motorola_lichengbei // Tuesday January 22, 2013 // motorcycle milestone-2013-1-22 // Baidu // success.

Appendix:

1. The encoding is specified for read/write operations, which does not seem to have any effect.

2. "\ r \ n" for line breaks so that the files opened and written with ue do not prompt conversion to DoS or something.

3. All the methods are searched out. Here we only combine them to fulfill our needs. Thanks to the original creators of these methods.

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.