\r\n 写入txt出现乱码

zhufenghappy 2009-04-29 03:52:21
\r\n 写入txt出现乱码,写入的东西多了个方块估计是乱码,代码如下,大家帮忙看下。还有我不用换行回车直接写字符串时,怎么写入的是“ --你好;”怎么多了空白呢?



import java.awt.HeadlessException;
import java.io.*;


public class TestA {

public static void main(String[] s) {


File filename = new File("d:\\test.txt");
String filein = "\r\n--你好;";

RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename,false);
mm.writeUTF(filein);

} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}

}

}
...全文
687 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
云上飞翔 2009-04-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jinxfei 的回复:]
writeUTF方法,首先,写入两个字节,这两个字节代表长度,表明本次方法调用将实际写入多少字节的数据
[/Quote]
答:这个回答说到点子上了.这就是那个方块的由来.
yof 2009-04-30
  • 打赏
  • 举报
回复

import java.awt.HeadlessException;
import java.io.*;


public class TestA {

public static void main(String[] s) {


File filename = new File("d:\\test.txt");
String filein = "\r\n--你好;";

RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename,false);
mm.write(filein.getBytes("UTF-8"));

} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}

}

}
jinxfei 2009-04-29
  • 打赏
  • 举报
回复

public final void writeUTF(String str)
throws IOException

Writes a string to the file using UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for each character.


从这个方法的说明我们可以看出:
writeUTF方法,首先,写入两个字节,这两个字节代表长度,表明本次方法调用将实际写入多少字节的数据。
hbgzg3006 2009-04-29
  • 打赏
  • 举报
回复
修改后片断如下:		String filein = "\r\n--你好;";
byte[]a=filein.getBytes();
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename, false);
// mm.writeUTF(filein);
mm.write(a);
jinxfei 2009-04-29
  • 打赏
  • 举报
回复
不好意思,我说得BOM头的问题有错。
用UE看,是00 12两个字符,目前还不知道是什么。


其实,用BufferedWriter也挺好的。
timmf 2009-04-29
  • 打赏
  • 举报
回复
首先注意看API中写的:
writeUTF
public final void writeUTF(String str) throws IOException
使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
首先,把两个字节从文件的当前文件指针写入到此文件,类似于使用 writeShort 方法并给定要跟随的字节数。此值是实际写出的字节数,而不是该字符串的长度。在该长度之后,按顺序输出该字符串的每个字符,并对每个字符使用 UTF-8 修改版编码。

指定者:
接口 DataOutput 中的 writeUTF
参数:
str - 要写入的字符串。
抛出:
IOException - 如果发生 I/O 错误。

然后看问题:用十六进制模式观看打印的文本文件,发现前四个是 00 0B 0D 0A 其中 0D 0A 即为回车和换行,那前面多的两个字节就是乱码了,API中说明了这两个字节是实际写出的字节数。

请运行如下代码:
public class HuanHangLuanMa3 {

public static void main(String[] args) throws Exception {

byte[] b = new byte[]{(byte)0x00, (byte)0x0B};
byte b1 = b[0];
byte b2 = b[1];
String s = new String(b);
System.out.println(s);
System.out.println(Byte.valueOf(b1).intValue());
System.out.println(Byte.valueOf(b2).intValue());

System.out.println("\r\n--你好;".getBytes().length);
}

}

运行结果:
方框(就是你所说的乱码)
0
11
9
此时你应该能明白了吧?
jinxfei 2009-04-29
  • 打赏
  • 举报
回复
并不是\r\n为乱码,而是writeUTF在文件中写入了BOM头。

不行你可以这样:
String filein = "abcdefg\r\n--你好;";
你会发现,所谓的乱码出现在abcdef前面。

BOM头,你可以google上查一下。
znyxq 2009-04-29
  • 打赏
  • 举报
回复
是writeUTF()方法的问题,可能是writeUTF方法改变字符串编码时出的错吧,你换成writeBytes()就没有你说的问题了。
djs36 2009-04-29
  • 打赏
  • 举报
回复

String filein = "\r\n--你好;";
filein = new String(filein.getBytes(), "gb2312");



gb2312,GBK,UTF-8
按需要选择
喊我满哥 2009-04-29
  • 打赏
  • 举报
回复
学习下。。。
zhufenghappy 2009-04-29
  • 打赏
  • 举报
回复
是widows,JDK版本是jdk1.5.0_07,是不是JDK版本低啊
hbgzg3006 2009-04-29
  • 打赏
  • 举报
回复
你什么系统下?不是widows吧?
十一种文件格式转换,四种内码转换,文件合并,文件分割,乱码修改,格式整理,文件更名,目录合并,广告删除,HTML 代码删除,自动排版,文本搜索替换,正则表达式搜索替换,块搜索替换,通配符搜索替换.全面支持 Unicode,Unicode Big Endian,UTF-8 格式文件,功能可扩展,支持批处理。文件体积小,纯绿色软件,不用安装直接运行。 用法 1 首先选择你的 txt/html 文件所在目录,该目录中的文件将显示在列表中 2 单击列表中的一个文件名来打开此文件 3 批处理就是一次性依次处理整个目录中的所有文件。 对于格式整理而言,通常情况下, 你只需使用"格式整理"就够了。 建议多进行一次“格式整理”处理。这样可以把广告清除得更彻底一些。 此外 巧用查找和替换功能,可以达到一些意想不到的效果。例如,当对一个文档进行了"格式整理"后,再进行如下替换:把“\r\n”全部替换成“\r\n ”(不含引号) 或者 把“\r\n”全部替换成“\r\n\r\n ”(不含引号) 呵呵,你自己看看处理效果吧! 另外,以上操作的逆操作是: 把“\r\n ”全部替换成“\r\n”(不含引号) 把“\r\n\r\n ”全部替换成“\r\n”(不含引号) 关于去除广告 你可以在你的 txt 文件所在的目录中创建一个名为 remove.txt 的文件,在其中写入你想要移除的广告内容,一行一条。 这样,txtFormat 会将 remove.txt 中的内容逐条、全部从你的 txt 文件中删除。 本文来自派派小说论坛 :http://www.paipaitxt.com/r4744229_u7118478/

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧