Import java. Io .*;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Public class testhuanhang {
/*
* It is applicable to converting UNIX line breaks/x0a of all files in a directory to/x0d/x0a on Windows.
* It is stored in another folder. Use different folder names.
* Parameter 1, source folder, parameter 2 destination folder, parameter 3 filtered regular expression. Parameter 4, new line breaks. Parameters 1, 2, and 3 must be specified
The format of parameter 4 is CRLF, Cr, or lf.
* Both the target folder and the original folder must exist in advance.
* Example of a command: Java testhuanhang. Work. xml CRLF
*/
Public static void main (final string [] ARGs ){
If (ARGs. Length <3 ){
System. Out. println ("Enter the folder containing the Unix line break file, and the folder to be placed after conversion! And the filtered regular ");
System. Exit (0 );
}
File srcfolder = new file (ARGs [0]);
File destfolder = new file (ARGs [1]);
If (! (Srcfolder. isdirectory () & destfolder. isdirectory ())){
System. Out. println ("the source folder and target folder must exist and are Directories! ");
System. Exit (0 );
}
If (srcfolder. compareto (destfolder) = 0 ){
System. Out. println ("the source folder and target folder cannot be the same! ");
System. Exit (0 );
}
If (ARGs. Length = 4 &&! (ARGs [3]. equalsignorecase ("CRLF") | ARGs [3]. equalsignorecase ("lf") | ARGs [3]. equalsignorecase ("cr "))){
System. Out. println ("Please specify the correct line delimiter! Such as CRLF, Cr, or lf ");
System. Exit (0 );
}
String Sline = "";
If (ARGs. Length = 3 ){
Sline = "/N ";
} Else if (ARGs [3]. equalsignorecase ("CRLF ")){
Sline = character. tostring (char) 13) + character. tostring (char) 10 );
} Else if (ARGs [3]. equalsignorecase ("cr ")){
Sline = character. tostring (char) 13 );
} Else {
Sline = character. tostring (char) 10 );
}
File [] SRCS = srcfolder. listfiles (New filenamefilter (){
Public Boolean accept (File Dir, string name ){
Matcher M = pattern. Compile (ARGs [2]). matcher (name );
If (M. Find ()){
Return true;
} Else {
Return false;
}
}
});
For (File SRC: SRCS ){
Bufferedreader BR = NULL;
Printwriter PW = NULL;
Try {
Try {
BR = new bufferedreader (New filereader (SRC ));
PW = new printwriter (new file (destfolder. getabsolutepath (), SRC. getname ()));
String line;
While (null! = (Line = Br. Readline ())){
PW. Print (LINE + Sline );
}
} Finally {
Try {
If (BR! = NULL ){
BR. Close ();
}
} Finally {
If (PW! = NULL ){
PW. Close ();
}
}
}
} Catch (exception e ){
}
}
}
}