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;
}