日期:2014-05-20  浏览次数:20913 次

javaSE 汉字编码问题
oracle的注释为--,而mysql的注释为#,
我这里有一张表,是oracle写的,我现在写个java把他的--转换成# ,
结果,代码ok,但是,被注释的中文乱码了,求教!

public class CommentOut_OracleToMySQL {
public static void main(String[] args) throws IOException {
_OracleToMySQL("C:/book.txt");
}
public static void _OracleToMySQL(String pathName) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(new File(pathName)));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/text.txt"));
String text = "";
String tmpReader = null;
while( null != (tmpReader=reader.readLine()) ){
tmpReader = new String(tmpReader.getBytes(),"UTF-8");
String[] split = tmpReader.split("--");
for( int i=0 ; i<split.length ; i++ ){
text = text+split[i]+"##";
}
text = text.substring(0, text.length()-2)+System.getProperty("line.separator");
text = new String(text.getBytes(),"UTF-8");
bw.write(text);
text = "" ;
}
bw.close();
reader.close();
}
}

------解决方案--------------------
UTF-8 改为 GBK试试先。
------解决方案--------------------
先看看你自己项目工作空间设置的是什么类型
------解决方案--------------------
book.txt 是什么编码的,使用InputStreamReader读入。
使用OutputStreamWriter写出。


------解决方案--------------------
你是哪里乱码了,是读进来的时候就乱码了,还是写出去的时候乱码。利用InputStreamReader来处理读取乱码,OutputStreamWriter来处理写入乱码
------解决方案--------------------
引用:
这是我的其中一张表:
Create table power(                                        --权限表
                 power_id numeric(9) primary key not null, --权限编号
                 user_id numeric(9)  not null,             --用户编号
                 power_role varchar(100) not null          --用户角色
                 );


我现在改了,好像也不行,请看代码,望指教问题出在哪:

import java.io.*;
public class CommentOut_OracleToMySQL {
public static void main(String[] args) throws IOException {
_OracleToMySQL("C:/book.txt");
}
public static void _OracleToMySQL(String pathName) throws IOException{
InputStreamReader inR = new InputStreamReader(new FileInputStream(pathName),"UTF-8");
BufferedReader reader = new BufferedReader(inR);
OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream("C:/text.txt"),"UTF-8");
BufferedWriter bw = new BufferedWriter(out);
System.out.println("按默认编码["+inR.getEncoding()+"]输入");
System.out.println("按默认编码["+out.getEncoding()+"]输出");
String text = "";
String tmpReader = null;
while( null != (tmpReader=reader.readLine()) ){
tmpReader = new String(tmpReader.getBytes(),"UTF-8");
String[] split = tmpReader.split("--");
for( int i=0 ; i<split.length ; i++ ){
text = text+split[i]+"##";
}
text = text.substring(0, text.length()-2)+System.getProperty("line.separator");
text = new String(text.getBytes(),"UTF-8");
bw.write(text);
text = "" ;
}
bw.close();
reader.close();
}
}

你代码既然能正确的替换和输出就是代码的逻辑是没有问题的,还是我上面说的你先看下你是哪里乱码的,是在读取文件的时候读进来的数据就已经是乱码的还是写文件数据写出去的时候变乱码了.你先把你while里面读到的tempReader这个打印出来看看是不是读的时候就已经乱码了,如果读的时候就乱码了那么就不能用utf-8来读了要还gbk来读,写出去也要换gbk来写
------解决方案--------------------
你确定文件的原始编码是GBK,还是UTF-8?
------解决方案--------------------
引用:
Quote: 引用: