Java line feed conversion

Source: Internet
Author: User

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 ){

}

}

 

 

}

}

Related Article

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.