Skip to content

Instantly share code, notes, and snippets.

@alexzhan
Created November 22, 2010 13:34
Show Gist options
  • Save alexzhan/709978 to your computer and use it in GitHub Desktop.
Save alexzhan/709978 to your computer and use it in GitHub Desktop.
To change the charset of one file name
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