开发一个彩信的客户端,发送文本文件和图片文件,图片发送正常,文本文件中的英文也发送正常,就是中文,手机接收到以后看不到中文,不是乱码,就是没有了,空格或者是直接消失。愁死了。号称是手机需要UTF-8编码的文件才能看,然后我就转啊转啊怎么就是不行,后来用Notepad++直接编辑UTF-8的文件,有BOM的,无BOM的,后来我自己往文件头加BOM,都是不行啊。请熟悉文件操作的大大们指教一下啊怎么安全高效的转换这个文件的编码啊。另有熟悉parlayx的吗有用这个开发增值业务的吗?一并求教了我。
解决方案 »
- 奇怪的问题
- 如 何 使 一 个 SWING 组 件 透 明。
- 小弟愚见,一个奇怪的问题
- 编译的问题
- SWING 问题,对于TABLE 行来说,如何能让里面装的checkbox 正确响应
- java调用c开发的动态链接库时,当动态链接库是指针时,在java程序中应如何相应改变
- 欢迎大家讨论java版的内存泄漏工具。
- 各位大神求解,今天在做JBUTTON时候遇到个问题
- 请问我的java程序在绘制一个GIF时汉字全是方框,请问怎么解决?linux下,我起了X了
- 这是什么错误,怎么解决啊??
- org.eclipse.jface.text cannot be resolved
- javascript 验证表单??哪个市斤给我看看错误在哪呢?我是新手。主要是邮箱验证这块不懂
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];
}
}
}
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");
}}