1.import java.io.File; 2.import java.io.FileOutputStream; 1.import java.io.UnsupportedEncodingException; 1. 2./** 1. * 2007-8-10 jyin at gomez dot com 2. */ 3.public class CharsetConvertor { 1. public static void main(String[] args) { 2. String str = "This is a test for *中网!@#$。,?"; 3. try { 4. File f = new File("D:/test.txt"); 5. FileOutputStream fio = new FileOutputStream(f); 6. String s = gbToUtf8(str); 7. fio.write(s.getBytes("UTF-8")); 8. fio.close(); 9. } 10. catch (Exception e) { 11. e.printStackTrace(); 12. } 13. } 14. 15. public static String gbToUtf8(String str) throws UnsupportedEncodingException { 16. StringBuffer sb = new StringBuffer(); 17. for (int i = 0; i < str.length(); i++) { 18. String s = str.substring(i, i + 1); 19. if (s.charAt(0) > 0x80) { 20. byte[] bytes = s.getBytes("Unicode"); 21. String binaryStr = ""; 22. for (int j = 2; j < bytes.length; j += 2) { 23. // the first byte 24. String hexStr = getHexString(bytes[j + 1]); 25. String binStr = getBinaryString(Integer.valueOf(hexStr, 16)); 26. binaryStr += binStr; 27. // the second byte 28. hexStr = getHexString(bytes[j]); 29. binStr = getBinaryString(Integer.valueOf(hexStr, 16)); 30. binaryStr += binStr; 31. } 32. // convert unicode to utf-8 33. String s1 = "1110" + binaryStr.substring(0, 4); 34. String s2 = "10" + binaryStr.substring(4, 10); 35. String s3 = "10" + binaryStr.substring(10, 16); 36. byte[] bs = new byte[3]; 37. bs[0] = Integer.valueOf(s1, 2).byteValue(); 38. bs[1] = Integer.valueOf(s2, 2).byteValue(); 39. bs[2] = Integer.valueOf(s3, 2).byteValue(); 40. String ss = new String(bs, "UTF-8"); 41. sb.append(ss); 42. } else { 43. sb.append(s); 44. } 45. } 46. return sb.toString(); 47. } 48. 49. private static String getHexString(byte b) { 50. String hexStr = Integer.toHexString(b); 51. int m = hexStr.length(); 52. if (m < 2) { 53. hexStr = "0" + hexStr; 54. } else { 55. hexStr = hexStr.substring(m - 2); 56. } 57. return hexStr; 58. } 59. 60. private static String getBinaryString(int i) { 61. String binaryStr = Integer.toBinaryString(i); 62. int length = binaryStr.length(); 63. for (int l = 0; l < 8 - length; l++) { 64. binaryStr = "0" + binaryStr; 65. } 66. return binaryStr; 67. } 68.}
按gb2312读字节。再用utf8写就行了呗
按gb2312读字符串。再用utf8写就行了呗
public static void conversion(String filename) throws Exception { File f = new File(filename); if (!f.exists()) return; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); byte read[] = new byte[bis.available()]; bis.read(read); bis.close(); byte write[] = new String(read).getBytes("utf8"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f)); bos.write(write); bos.flush(); bos.close(); }这样么?
1.String text = new String(text.getBytes("gb2312"), "utf8");就是这样.
2.import java.io.FileOutputStream;
1.import java.io.UnsupportedEncodingException;
1.
2./**
1. * 2007-8-10 jyin at gomez dot com
2. */
3.public class CharsetConvertor {
1. public static void main(String[] args) {
2. String str = "This is a test for *中网!@#$。,?";
3. try {
4. File f = new File("D:/test.txt");
5. FileOutputStream fio = new FileOutputStream(f);
6. String s = gbToUtf8(str);
7. fio.write(s.getBytes("UTF-8"));
8. fio.close();
9. }
10. catch (Exception e) {
11. e.printStackTrace();
12. }
13. }
14.
15. public static String gbToUtf8(String str) throws UnsupportedEncodingException {
16. StringBuffer sb = new StringBuffer();
17. for (int i = 0; i < str.length(); i++) {
18. String s = str.substring(i, i + 1);
19. if (s.charAt(0) > 0x80) {
20. byte[] bytes = s.getBytes("Unicode");
21. String binaryStr = "";
22. for (int j = 2; j < bytes.length; j += 2) {
23. // the first byte
24. String hexStr = getHexString(bytes[j + 1]);
25. String binStr = getBinaryString(Integer.valueOf(hexStr, 16));
26. binaryStr += binStr;
27. // the second byte
28. hexStr = getHexString(bytes[j]);
29. binStr = getBinaryString(Integer.valueOf(hexStr, 16));
30. binaryStr += binStr;
31. }
32. // convert unicode to utf-8
33. String s1 = "1110" + binaryStr.substring(0, 4);
34. String s2 = "10" + binaryStr.substring(4, 10);
35. String s3 = "10" + binaryStr.substring(10, 16);
36. byte[] bs = new byte[3];
37. bs[0] = Integer.valueOf(s1, 2).byteValue();
38. bs[1] = Integer.valueOf(s2, 2).byteValue();
39. bs[2] = Integer.valueOf(s3, 2).byteValue();
40. String ss = new String(bs, "UTF-8");
41. sb.append(ss);
42. } else {
43. sb.append(s);
44. }
45. }
46. return sb.toString();
47. }
48.
49. private static String getHexString(byte b) {
50. String hexStr = Integer.toHexString(b);
51. int m = hexStr.length();
52. if (m < 2) {
53. hexStr = "0" + hexStr;
54. } else {
55. hexStr = hexStr.substring(m - 2);
56. }
57. return hexStr;
58. }
59.
60. private static String getBinaryString(int i) {
61. String binaryStr = Integer.toBinaryString(i);
62. int length = binaryStr.length();
63. for (int l = 0; l < 8 - length; l++) {
64. binaryStr = "0" + binaryStr;
65. }
66. return binaryStr;
67. }
68.}
File f = new File(filename);
if (!f.exists())
return;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
byte read[] = new byte[bis.available()];
bis.read(read);
bis.close();
byte write[] = new String(read).getBytes("utf8");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
bos.write(write);
bos.flush();
bos.close();
}这样么?