站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题我想把一个文件转码成utf8的,但是文件中出现了乱码,希望高手指点(内附代码)

我想把一个文件转码成utf8的,但是文件中出现了乱码,希望高手指点(内附代码)

摘要: 我想把一个文件转码成utf8的,但是文件中出现了乱码,希望高手指点(内附代码)


我想把一个生成的xml文件转码成utf8的,但是经过处理后的文件出现了乱码。
请高手帮忙
----------------------------------------------
package middlegen.plugins.eway;

import java.io.IOException;
import java.io.RandomAccessFile;

public class ChangeFile2UTF {
private String file;

public static void main(String[] args) throws Exception {
ChangeFile2UTF changer = new ChangeFile2UTF();
//123.xml中包含中文
changer.setFile("D:/123.xml");
changer.changeFileCode();
}

public void changeFileCode() throws IOException {
RandomAccessFile rFile = new RandomAccessFile(file, "rw");

long pointer = 0;
String line =rFile.readLine();
System.out.println(line);

while(line != null) {
rFile.seek(pointer);
//rFile.writeUTF(new String(line.getBytes("ISO-8859-1"), "UTF-8"));
//rFile.writeUTF(line + "/n/r");
//rFile.writeUTF(line);
if(hasChinese(line)) {
rFile.writeUTF(line + "/r/n");
} else {
line = rFile.readLine();
}

pointer = rFile.getFilePointer();
line = rFile.readLine();
System.out.println(line);
}
}

private boolean hasChinese(String val) {
byte[] bytes = val.getBytes();

for(int i = 0; i < bytes.length; i++) {
if(bytes[i] < 0) {
return true;
}
}
return false;
}

public String getFile() {
return file;
}

public void setFile(String file) {
this.file = file;
}
}



编码转换后,长度不一样了,不能这样做
先读取源文件,转换编码后写入新文件,删除源文件,将新文件重命名为源文件


楼上的有道理。
rFile.writeUTF(new String(line.getBytes("ISO-8859-1"), "UTF-8"));
这句,ISO-8859-1是不是应该改成原有文件的编码啊?



谢谢楼上两位的回复
to 北极人
我试过把内容读出来写入新文件,但是在文件最开头就出现了乱码(两个字符的乱码,后面正常.然后每行开头都有乱码).

to 孙超
你说的那行已经被注释了,直接用rFile.writeUTF(line);就可以转换编码.

我写了一个测试类读取转码过的文件的指定行,并把这行转换成byte数组发现转码后的文件在开头的地方多了byte(0)和byte(38)
在eclipse控制台打出来的乱码复制到这里就没有了就不再粘贴了.
------------------
测试类的代码如下:
------------------
package middlegen.plugins.eway;

import java.io.RandomAccessFile;

public class FileLineCompare {
private static String filePath1 = "D:/biz-focus5.0/conf/eway-home/pagemeta/functionManager.xml";
private static String filePath2 = "D:/java ide/eclipse/workspace/add comment/file/personalWork.xml";
private static String filePath3 = "D:/java ide/eclipse/workspace/add comment/file/123.xml";

private static int rowNo = 1;

public static void main(String[] args) throws Exception {
RandomAccessFile rFile1 = new RandomAccessFile(filePath1, "r");
RandomAccessFile rFile2 = new RandomAccessFile(filePath2, "r");
RandomAccessFile rFile3 = new RandomAccessFile(filePath3, "r");

String line1 = readLine(rFile1);
String line2 = readLine(rFile2);
String line3 = readLine(rFile3);

System.out.println(filePath1);
printStringAsBytes(line1);
System.out.println(filePath2);
printStringAsBytes(line2);
System.out.println(filePath3);
printStringAsBytes(line3);

rFile1.close();
rFile2.close();
rFile3.close();
}

private static void printStringAsBytes(String line) {
if(line == null) return;
byte[] bs = line.getBytes();
System.out.println(line);

for(int i = 0; i < bs.length; i++) {
System.out.print(bs[i] + " ");
}

System.out.println();
}

private static String readLine(RandomAccessFile file) throws Exception {
String line = null;
for(int i = 0; i < rowNo; i++) {
line = file.readLine();
}
return line;
}
}



先看一下你的xml文件编码是什么。
我怀疑不是utf-8编码的。

=================================================================
我试过把内容读出来写入新文件,但是在文件最开头就出现了乱码
=================================================================
utf-8文件开头是两个特殊字符,必须的。



帮顶


to eureka0891(迷茫中...)
源xml文件的首行是

<?xml version="1.0" encoding="UTF-8"?>



不知我所提供信息是eureka0891(迷茫中...)兄所指,惭愧中


有<?xml version="1.0" encoding="UTF-8"?>一般都是
但我怀疑你这儿不是。

其实你这儿没这么麻烦的。
你这儿用不着RandomAccessFile、就算要用,你写的方法也有毛病,首先要读出来没问题,然后才是写

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;

public class ChangeFile2UTF {
private String file;

public static void main(String[] args) throws Exception {
ChangeFile2UTF changer = new ChangeFile2UTF();
// 123.xml中包含中文
changer.setFile("C:/Documents and Settings/t.hou/workspace/test/123.xml");
changer.changeFileCode();
}

public void changeFileCode() throws IOException {
BufferedWriter writer = null;
BufferedReader reader = null;

try {
writer= new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Documents and Settings/t.hou/workspace/test/123-2.xml"), "UTF-8"));
reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), "GB2312"));

String sep = System.getProperty("line.separator");
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
writer.write(line);
writer.write(sep);
}
} finally {
if(writer != null){
writer.close();
}
if(reader != null){
reader.close();
}
}
}


public String getFile() {
return file;
}

public void setFile(String file) {
this.file = file;
}
}


有<?xml version="1.0" encoding="UTF-8"?>一般都是
但我怀疑你这儿不是。

-----------------------------------------------------
我把文件用eclipse打开,文件属性页里面显示的是utf8(我不知道怎么用其他的办法查看文件的字符集).用记事本和editplus打开正常显示中文,用ultraedit打开则乱码.
另外上面的代码执行完成后源文件被修改而目标文件中无内容:(


如果已经是utf-8的,还用转码干嘛?
要看是否是utf-8将这个打进去就知道了 ②
看显示是否正常。

另外上面的代码执行完成后源文件被修改而目标文件中无内容
-----------------------------------------------------
reader=new BufferedReader(new InputStreamReader(new FileInputStream(file), "GB2312"));
你的文件已经是utf-8
估计有Exception


虽然eclipse里显示是utf8,但我觉得不是,或者文件里面有问题。我直接用ie打开这个xml显示这个xml提示如下错误信息:
--------------------
文本内容中发现无效字符。处理资源 'file:///D:/java ide/eclipse/workspace/add comment/fileBackup/personalWork.xml' 时出错。第 3 行,位置: 13

------------------------
第三行只有

-----------------------
谢谢 eureka0891(迷茫中...) 的帮助


↑返回目录
前一篇: 排序的问题
后一篇: 一个小排序问题。。。