在java中gbk怎么转utf8编码
java中,编码是字节转字符的时候产生的不同映射造成的差异。那我们就可以利用这一点实现。例如,我们有一个gbk编码格式的字符串,那我们先把它的字节得到。
String s = “abc”;
byte[] b = s.getBytes();
再将其转为需要的编码格式。如utf-8
String newStr = new String(b,”utf-8″)
binggo~!!
java中GBK编码格式转成UTF8,用一段方法实现怎么做
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
public class gbk2utf8 {
public static void main(String[] args) throws InterruptedException, IOException {
gbk2utf8 tt = new gbk2utf8();
tt.copyAll(“E:\\mine\\GitHub\\mytest\\myJar\\src”, “E:/test”);
}
public void copyAll(String dir,String des) throws IOException{
File parent = new File(dir);
System.err.println(parent.getAbsolutePath());
String[] allFile = parent.list();
for (String string : allFile) {
File file = new File(dir+”/”+string);
if(file.isFile()){
this.copy(file,des);
}else{
File newDes = new File(des+”/”+string);
if(!newDes.exists()) newDes.mkdir();
this.copyAll(dir+”/”+string,des+”/”+string);
}
}
}
public void copy(File file,String des) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName(“gbk”)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(des+”/”+file.getName()), Charset.forName(“UTF-8”)));
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) {
bw.write(lineTxt);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}
Java中如何把GBK编码的字符串转成utf-8编码
我先解释一下我对你的问题的理解
:
你的问题有点笼统
可以这么理解两个意思
:
一
,你是想吧一个gbk编码的字符串用utf-8格式来编码
String
s
=
new
String
(“全国”.getBytes(“GBK”),”GBK”);
虽然这个时候的s是GBK编码的
但是s还是中文字符的
所以
上面这位同胞用
new(s.getBytes(“GBK”),”UTF-8″)的方式肯定是错的
应该用
String
s1
=
new
String
(s.getBytes(“UTF-8″),”UTF-8”)
这样s1就是UTF-8编码的字符串了
二,如果是遇见乱码问题
比如
String
s
=
new
String
(“全国”.getBytes(“GBK”),”GBK”);
这样的s就是gbk编码的
现在你要把GBK格式的编码用UTF-8去编码
肯定就会出现乱码
String
s2
=
new
String(s.getBytes(“GBK”),”UTF-8″);
这个s2肯定是乱码的
而且这样就根本解不出来了
系统出现的乱码显示的时候才会出现编码错误
所以
一般出现乱码
只要把输入流换个编码解析一下就ok了
还有什么不明白
可以问我
java 怎么gbk转码utf-8
方法一:
思路:先转为Unicode,然后转为GBK
String utf8 = new String(t.getBytes( “UTF-8”));
System.out.println(utf8);
String unicode = new String(utf8.getBytes(),”UTF-8″);
System.out.println(unicode);
String gbk = new String(unicode.getBytes(“GBK”));
System.out.println(gbk);
方法二:
public static void main(String[] args) {
String str=”字符串编码转换”;
try {
byte[] temp=str.getBytes(“utf-8”);//这里写原编码方式
byte[] newtemp=new String(temp,”utf-8″).getBytes(“gbk”);//这里写转换后的编码方式
String newStr=new String(newtemp,”gbk”);//这里写转换后的编码方式
System.out.println(newStr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java怎么将gbk编码的文件转成utf-8编码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
public class gbk2utf8 {
public static void main(String[] args) throws InterruptedException, IOException {
gbk2utf8 tt = new gbk2utf8();
tt.copyAll(“E:\\mine\\GitHub\\mytest\\myJar\\src”, “E:/test”);
}
public void copyAll(String dir,String des) throws IOException{
File parent = new File(dir);
System.err.println(parent.getAbsolutePath());
String[] allFile = parent.list();
for (String string : allFile) {
File file = new File(dir+”/”+string);
if(file.isFile()){
this.copy(file,des);
}else{
File newDes = new File(des+”/”+string);
if(!newDes.exists()) newDes.mkdir();
this.copyAll(dir+”/”+string,des+”/”+string);
}
}
}
public void copy(File file,String des) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName(“gbk”)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(des+”/”+file.getName()), Charset.forName(“UTF-8”)));
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) {
bw.write(lineTxt);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
}
}