首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

javaSE 汉字编码有关问题

2014-01-17 
javaSE 汉字编码问题oracle的注释为--,而mysql的注释为#,我这里有一张表,是oracle写的,我现在写个java把他

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: 引用:


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


测试了下,应该是在读入的时候就是乱码了,可是,如果不是因为编码不对,我们又何须转码,所以我觉得问题似乎不出在这。望指教

你u8读取乱码了那就用gbk来读,用gbk来写
[解决办法]
    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]+"##";
            }
            System.out.println(text);
            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();
    }
}
去掉你的转码就好了
[解决办法]
我们的能看懂的文件都是字符形式的,编码是字节到字符的一个转换规则,不管你怎么用怎么写,你读的时候都用字节去读,然后包装成字符,从字节转到字符的时候加上编码即可。这个编码就是原文件的编码。这样写出来就不乱码了。

[解决办法]
楼主看看,

/***
 * oracle的注释为--,而mysql的注释为#, 我这里有一张表,是oracle写的,我现在写个java把他的--转换成# ,
 * 结果,代码ok,但是,被注释的中文乱码了,求教!
 */
static void s6(){
try {
//新建的txt默认编码格式GBK 编码方式的,所以这里读取的必须设置成GBK方式
InputStreamReader inR = new InputStreamReader(new FileInputStream("E:/a.txt"),"GBK");
BufferedReader reader = new BufferedReader(inR);
OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream("E:/b.txt"),"GBK");

//如果想使用UTF-8,请将txt文件另存为,设置成UTF-8编码方式
//InputStreamReader inR = new InputStreamReader(new FileInputStream("E:/a.txt"),"UTF-8");
//BufferedReader reader = new BufferedReader(inR);
//OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream("E:/b.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()) ){
    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");
    bw.write(text);
    text = "" ;
}
bw.close();
reader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

[解决办法]
楼猪为什么要用split把他拆开,然后又把它串在一起啊,不可以直接replace掉么
[解决办法]
tmpReader = new String(tmpReader.getBytes(),"UTF-8");

这一行代码的意思是,使用UTF-8,tmpReader的字节进行编码,那也就是说,tmpReader里面的字节应该是UTF-8编码才能正常处理。因此,先确定你的文件是不是UTF-8编码的。用记事本打开那个txt文件,然后保存的时候选择编码为utf-8即可。
写入一样的道理。

热点排行