当前位置: 技术问答>java相关
300分救助中文问题的解决方案(分两次付清)
来源: 互联网 发布时间:2015-03-31
本文导语: jdk1.3.1 windows2000 server 实在是被搞糊涂了,这里解决了,那里又有问题,比如: 某个Writer写入文件时候好象转化为本地的Asscii码了,但是用某个Reader读出后又是乱码(好象又被转化成Unicode了) 什么8位,16位,什么字...
jdk1.3.1
windows2000 server
实在是被搞糊涂了,这里解决了,那里又有问题,比如:
某个Writer写入文件时候好象转化为本地的Asscii码了,但是用某个Reader读出后又是乱码(好象又被转化成Unicode了)
什么8位,16位,什么字节转化,什么ISO编码集,真是糊涂,望哪位大虾不吝指教
从理论的角度给个解决办法(不管遇到什么情况)
(总不能装个中文开发包吧)
分数不够还可以再加
windows2000 server
实在是被搞糊涂了,这里解决了,那里又有问题,比如:
某个Writer写入文件时候好象转化为本地的Asscii码了,但是用某个Reader读出后又是乱码(好象又被转化成Unicode了)
什么8位,16位,什么字节转化,什么ISO编码集,真是糊涂,望哪位大虾不吝指教
从理论的角度给个解决办法(不管遇到什么情况)
(总不能装个中文开发包吧)
分数不够还可以再加
|
研究一下。shmilu@sina.com
/**
convert strings
*/
import java.io.*;
public class inputtest {
public static void main(String[] args) {
String str = "u00a3u00b1u00a3u00b2u00a3u00b3u00a3u00b4u00a3u00b5u00a3u00b6u00a3u00b7u00a3";
str +="u00b8u00a3u00b9u00a3u00b0";
str +="u00a3u00c1u00a3u00c2u00a3u00c3u00a3u00c4u00a3u00c5u00a3u00c6u00a3u00c7u00a3";
str +="u00c8u00a3u00c9u00a3u00cau00a3u00cbu00a3u00ccu00a3u00cdu00a3u00ceu00a3u00cf";
str +="u00a3u00d0u00a3u00d1u00a3u00d2u00a3u00d3u00a3u00d4u00a3u00d5u00a3u00d6u00a3";
str +="u00d7u00a3u00d8u00a3u00d9u00a3u00da";
str +="u00a3u00e1u00a3u00e2u00a3u00e3u00a3u00e4u00a3u00e5u00a3u00e6u00a3u00e7u00a3";
str +="u00e8u00a3u00e9u00a3u00eau00a3u00ebu00a3u00ecu00a3u00edu00a3u00eeu00a3u00ef";
str +="u00a3u00f0u00a3u00f1u00a3u00f2u00a3u00f3u00a3u00f4u00a3u00f5u00a3u00f6u00a3";
str +="u00f7u00a3u00f8u00a3u00f9u00a3u00fa";
System.out.println(str);
String outfile = null;
args = new String[2];
args[0] = "c:/windows/temp/sn.html";
args[1] = "c:/windows/temp/sn1.html";
try { convert(args[0], args[1], "GB2312", "Big5"); } // or "BIG5"
catch (Exception e) {
System.out.print(e.getMessage());
System.exit(1);
}
}
public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingException
{
// set up byte streams
InputStream in;
if (infile != null) in = new FileInputStream(infile);
else in = System.in;
OutputStream out;
if (outfile != null) out = new FileOutputStream(outfile);
else out = System.out;
// Use default encoding if no encoding is specified.
if (from == null) from = System.getProperty("file.encoding");
if (to == null) to = System.getProperty("file.encoding");
// Set up character stream
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
// Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode,, and the OutputStreamWriter
// converts from Unicode to the output encoding. Characters that cannot be
// represented in the output encoding are output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1)
w.write(buffer, 0, len);
r.close();
w.flush();
w.close();
}
}
/**
convert strings
*/
import java.io.*;
public class inputtest {
public static void main(String[] args) {
String str = "u00a3u00b1u00a3u00b2u00a3u00b3u00a3u00b4u00a3u00b5u00a3u00b6u00a3u00b7u00a3";
str +="u00b8u00a3u00b9u00a3u00b0";
str +="u00a3u00c1u00a3u00c2u00a3u00c3u00a3u00c4u00a3u00c5u00a3u00c6u00a3u00c7u00a3";
str +="u00c8u00a3u00c9u00a3u00cau00a3u00cbu00a3u00ccu00a3u00cdu00a3u00ceu00a3u00cf";
str +="u00a3u00d0u00a3u00d1u00a3u00d2u00a3u00d3u00a3u00d4u00a3u00d5u00a3u00d6u00a3";
str +="u00d7u00a3u00d8u00a3u00d9u00a3u00da";
str +="u00a3u00e1u00a3u00e2u00a3u00e3u00a3u00e4u00a3u00e5u00a3u00e6u00a3u00e7u00a3";
str +="u00e8u00a3u00e9u00a3u00eau00a3u00ebu00a3u00ecu00a3u00edu00a3u00eeu00a3u00ef";
str +="u00a3u00f0u00a3u00f1u00a3u00f2u00a3u00f3u00a3u00f4u00a3u00f5u00a3u00f6u00a3";
str +="u00f7u00a3u00f8u00a3u00f9u00a3u00fa";
System.out.println(str);
String outfile = null;
args = new String[2];
args[0] = "c:/windows/temp/sn.html";
args[1] = "c:/windows/temp/sn1.html";
try { convert(args[0], args[1], "GB2312", "Big5"); } // or "BIG5"
catch (Exception e) {
System.out.print(e.getMessage());
System.exit(1);
}
}
public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingException
{
// set up byte streams
InputStream in;
if (infile != null) in = new FileInputStream(infile);
else in = System.in;
OutputStream out;
if (outfile != null) out = new FileOutputStream(outfile);
else out = System.out;
// Use default encoding if no encoding is specified.
if (from == null) from = System.getProperty("file.encoding");
if (to == null) to = System.getProperty("file.encoding");
// Set up character stream
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
// Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode,, and the OutputStreamWriter
// converts from Unicode to the output encoding. Characters that cannot be
// represented in the output encoding are output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1)
w.write(buffer, 0, len);
r.close();
w.flush();
w.close();
}
}
|
以系统默认编码方式编译.
你什么操作系统?你的代码在我的中文win2000一切正常.
你什么操作系统?你的代码在我的中文win2000一切正常.
|
希望能帖出你的原代码,如果原代码太长可以帖中文出问题那部分,我前段时间也是被JAVA中文问题捆饶了很久,网上有些文章是专门针对JAVA中文问题的,不过好多都是针对JSP的,我觉得JAVA中文问题要具体情况具体分析,我那个问题是UDP包收发中文的问题,依靠网上的文章没有解决,后来还是自己解决,反正说来说去还是字符编码的问题。
|
看看原代码现呀!!
|
import java.io.*;
public class Convert{
public static String unicodeToGB(String strIn){
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}catch(UnsupportedEncodingException e){}
return strOut;
}
public static String gBToUnicode(String strIn){
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))return strIn;
try{
byte[] b = strIn.getBytes("ISO8859_1");
strOut = new String(b,"GBK");
}catch(Exception e){}
return strOut;
}
}
在写入时:用Convert.unicodeToGB(String s)将于写入的转换为gb(如果是中文平台);
在读出时:用Convert.gBToUnicode(String s)将其转换为java的UNICODE即可。
public class Convert{
public static String unicodeToGB(String strIn){
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}catch(UnsupportedEncodingException e){}
return strOut;
}
public static String gBToUnicode(String strIn){
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))return strIn;
try{
byte[] b = strIn.getBytes("ISO8859_1");
strOut = new String(b,"GBK");
}catch(Exception e){}
return strOut;
}
}
在写入时:用Convert.unicodeToGB(String s)将于写入的转换为gb(如果是中文平台);
在读出时:用Convert.gBToUnicode(String s)将其转换为java的UNICODE即可。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。