Atitit. Software Development Concept Description--io System Area--Special file name save best practices file name encoding ... Filenameencode 1.1. Not a page title saved to each file when there are invalid characters of the problem ... 1

Source: Internet
Author: User
Tags parent directory urlencode

Atitit. Software Development Concept Description--io System Area--Special file name save best practices file name encoding ... Filenameencode

1.1. The problem of invalid characters when the page title is saved as a file. 1

1.2. Replace with Chinese full-width symbol (recommended, readability is best) 1

1.3. Use Escape (recommended) ... 1

1.4. Use the UrlEncode mode (simplest, readability generally) 1

2. Code 1

2.1. code--full-width symbol 1

1.1. The problem of invalid characters when a page title is saved as a file .

Usually two processing methods::: Replace into a space or use escape (recommended) ...

Invalid file name for Windows characters .../\:* <>\ "| Slash, backslash, colon, asterisk, question mark, left and right angle brackets double quote vertical slash

Angle brackets, double quotes, tree bars ... And ... "." A point and a "..." The two points are used to denote both the current directory and the parent directory, so they cannot be used as filenames

Linux is basically only a backslash ...

1.2. Replace with Chinese full-width symbol (recommended, readability is best) 1.3. use Escape (recommended) ...  1.4. Use the UrlEncode mode (simplest, readability generally) 2. Code 2.1. code--Full-width symbol

/hislog/src/main/java/com/attilax/io/fileservice.java

import org.apache.commons.io.FileUtils;

Public class Fileservice {

Public Static void Main (string[] args) throws IOException {

String oriname= "C:\\0wkspc\\hislog\\src\\main\\java\\com\\attilax\\io\\filenameencodetest.txt";

String T=fileutils.readfiletostring (new File (Oriname));

String newfilename=filenameencode_readablebest (t);

Fileutils.writestringtofile (new File ("c:\\0wkspc\\hislog\\src\\main\\java\\com\\attilax\\io\\" + newfilename+ ". txt"), "data");

System. out. println ("--");

}

/**

* but cant convert to ori

* @param Filenameori

* @return

*/

Public Static String Filenameencode_readablebest (String Filenameori) {

Filenameori=filenameori.replaceall ("\\\\", ",");

Filenameori=filenameori.replaceall ("\\/", ",");

Filenameori=filenameori.replaceall ("\\|", ",");

Filenameori=filenameori.replaceall ("\\*", ". ");

Filenameori=filenameori.replaceall ("\ \", ". ");

Filenameori=filenameori.replaceall ("\" "," "");

Filenameori=filenameori.replaceall ("<", "" ");

Filenameori=filenameori.replaceall (">", "" ");

Filenameori=filenameori.replaceall (":", ":");

return Filenameori;

}

Public Static String Filenameencode (String Filenameori) {

/\:* <>\ "|

if (Filenameori.equals ("."))

return "%2E";

if (Filenameori.equals (".."))

return "%2E%2E";

Map<string, string> MP = (map<string, string>) new closurenoexcpt () {

@Override

Public Object Execute (final object arg0) {

Final Map<string, string> MP = new hashmap<string, string> () {

{

this. put ("*", "%2a");

}

};

Final String[] as = Strutil.splitbynone ("/\\:?<>\" | ");

for (final String s:as) {

Try {

Mp.put (S, Urlencoder.encode (S, "utf-8"));

} catch (final unsupportedencodingexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

return MP;

}

}.execute (null);

String[] as = Strutil.splitbynone (Filenameori);

String fname2 = "";

for (String S:as) {

Fname2 + = Mp.get (s) = = null ? S:mp.get (s);

}

return fname2;

}

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.