Created
November 22, 2010 13:34
-
-
Save alexzhan/709978 to your computer and use it in GitHub Desktop.
To change the charset of one file name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
//import org.apache.commons.io.FileUtils; | |
public class TestFileCharSet { | |
protected static String urlEncode(String string) { | |
try { | |
return URLEncoder.encode(string,"GBK"); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} return null; | |
} | |
public static void main(String[] args) throws IOException { | |
File file = new File("/home/alex/Desktop/我的"); | |
System.err.println(file.getName()); | |
String destFileString = urlEncode(file.getName()); | |
System.err.println(destFileString); | |
// FileUtils.copyFile(file, new File("/home/alex/Desktop/" + destFileString)); | |
System.err.println(file.renameTo(new File(file.getParent() + File.separator + destFileString))); | |
// System.err.println(file.renameTo(new File(destFileString)));//wrong way | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment