当前位置: 技术问答>java相关
帮帮忙,老大催得太急,JAVA中把繁体转换成简体,哪位兄弟救救我
来源: 互联网 发布时间:2015-06-10
本文导语: public class test { public test() { } public static void main(String[] args) { String s = "歷史、穩定歷史、穩定歷史、穩定歷史、穩定"; System.out.println(s); String y = null; try{ y=new String(s.getB...
public class test {
public test() {
}
public static void main(String[] args) {
String s = "歷史、穩定歷史、穩定歷史、穩定歷史、穩定";
System.out.println(s);
String y = null;
try{
y=new String(s.getBytes(""),"GBK");
System.out.println(y);
}catch(Exception e){
}
}
}
上面程序在JB5中运行还是输出繁体,换成gb2312,输出“????????”
我知道有高人写过相关的转换代码,但找不到了。
谁能贴出来,马上给分
|
import java.io.*;
import java.util.*;
public class gb2big5 {
static int iCharNum=0;
public static void main(String[] args) {
System.out.println("Input GB2312 file, output Big5 file.");
if (args.length!=2) {
System.err.println("Usage: jview gb2big5 gbfile big5file");
System.exit(1);
String inputString = readInput(args[0]);
writeOutput(inputString,args[1]);
System.out.println("Number of Characters in file: "+iCharNum+".");
}
static void writeOutput(String str, String strOutFile) {
try {
FileOutputStream fos = new FileOutputStream(strOutFile);
Writer out = new OutputStreamWriter(fos, "Big5");
out.write(str);
out.close();
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
static String readInput(String strInFile) {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(strInFile);
InputStreamReader isr = new InputStreamReader(fis, "GB2312");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
iCharNum += 1;
buffer.append((char)ch);
}
in.close();
return buffer.toString();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
出自http://www-900.ibm.com/developerWorks/cn/java/java_chinese/index.shtml
big5 -> gb只要反过来写一下就行了
import java.util.*;
public class gb2big5 {
static int iCharNum=0;
public static void main(String[] args) {
System.out.println("Input GB2312 file, output Big5 file.");
if (args.length!=2) {
System.err.println("Usage: jview gb2big5 gbfile big5file");
System.exit(1);
String inputString = readInput(args[0]);
writeOutput(inputString,args[1]);
System.out.println("Number of Characters in file: "+iCharNum+".");
}
static void writeOutput(String str, String strOutFile) {
try {
FileOutputStream fos = new FileOutputStream(strOutFile);
Writer out = new OutputStreamWriter(fos, "Big5");
out.write(str);
out.close();
}
catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}
}
static String readInput(String strInFile) {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(strInFile);
InputStreamReader isr = new InputStreamReader(fis, "GB2312");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
iCharNum += 1;
buffer.append((char)ch);
}
in.close();
return buffer.toString();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
出自http://www-900.ibm.com/developerWorks/cn/java/java_chinese/index.shtml
big5 -> gb只要反过来写一下就行了
|
我感觉用gb2312应该没问题的
是不是你的系统不支持显示简体中文呀
实际上是转换过来了,只不过是没显示正常而已
是不是你的系统不支持显示简体中文呀
实际上是转换过来了,只不过是没显示正常而已
|
你看看这个行不行:
new String(s.getBytes("ISO8859-1"),"gb2312");
new String(s.getBytes("ISO8859-1"),"gb2312");