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.