开发一个彩信的客户端,发送文本文件和图片文件,图片发送正常,文本文件中的英文也发送正常,就是中文,手机接收到以后看不到中文,不是乱码,就是没有了,空格或者是直接消失。愁死了。号称是手机需要UTF-8编码的文件才能看,然后我就转啊转啊怎么就是不行,后来用Notepad++直接编辑UTF-8的文件,有BOM的,无BOM的,后来我自己往文件头加BOM,都是不行啊。请熟悉文件操作的大大们指教一下啊怎么安全高效的转换这个文件的编码啊。另有熟悉parlayx的吗有用这个开发增值业务的吗?一并求教了我。

解决方案 »

  1.   

    转换的代码请指教。。package client;import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;public class ToUTF8 {
    private static final byte[] UTF8 = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; public static void ChangeToUTF8(File fileName) throws IOException {
    try {
    FileInputStream fin = new FileInputStream(fileName);
    // BufferedInputStream buff = new BufferedInputStream(new
    // InputStream(fin, "UTF-8"));
    byte[] buffer = new byte[1024];
    int size = fin.read(buffer);
    fin.close();
    byte[] b = new byte[size + 3];
    b = buffer;
    // String strTempOne = new String(b);
    //  String strTempFour = new String(b, "iso-8859-1");
    // // String strTempTwo = new String(b, "GBK");
    String strTempThree = new String(b, "gb2312");//  String str = new String(b, "UTF-8");
    b = strTempThree.getBytes("UTF-8");
    // addBOM(fileName);
    // plusBOM(b);
    FileOutputStream fout = new FileOutputStream(fileName);
    fout.write(b);
    fout.flush();
    fout.close();
    //  System.out.println(str); // ����
    // System.out.println(strTempOne);
    //  System.out.println(strTempFour);
    // // System.out.println(strTempTwo);
    System.out.println(strTempThree); } catch (FileNotFoundException ex) {
    System.out.println(ex.toString());
    } } private static void addBOM(File fileName) throws IOException {
    FileOutputStream fout = new FileOutputStream(fileName);
    fout.write(UTF8);
    fout.flush();
    fout.close();
    } private static void plusBOM(byte[] byteName) {
    for (int i = 3; i < byteName.length; ++i) {
    byteName[i - 3] = byteName[i];
    }
    }
    }
      

  2.   

    import java.io.*;public class AsciiToUtf8 {
    public static void change(String srcFile,String destFile){
    InputStreamReader reader=null;
    OutputStreamWriter writer=null;

    try {
    reader=new InputStreamReader(new FileInputStream(srcFile),"GBK");
    writer=new java.io.OutputStreamWriter(new FileOutputStream(destFile),"UTF8");
    String line=null;
    char[] temp=new char[1024];
    int chars=-1;
    while ((chars=reader.read(temp))!=-1){
    writer.write(temp, 0, chars);
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally{
    try {
    reader.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    writer.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    change("e:/temp/1234.txt","e:/temp/1234_utf8.txt");
    }}