\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();
}
}
} }}
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();
}
}
} }}
解决方案 »
- 关于Java协变返回类型的一个问题
- java编译,提示JDK版本错误!
- try语句的问题
- jndi的绑定和取出问题
- 跪求哪位高手能帮忙把上面的部分按钮的功能不上呀!我是初学者,只写了放大缩小和还原 这三个按钮的功能。还请各位大大留心下!
- 关于 static 方法的引用
- Java2SDK v 1.2.1可以在XP下用么?怎么加那个PATH?
- Javamail 的地址问题,大家进来看看!…………
- 请教襄阳太守郭靖一个Socket的“见龙在田”的心法.
- Arraylist怎么实现我录入的 全部信息出来呢??
- 怎么样把中文转成%4e9a%%9a6c%%900a%这样的格式存储
- 登录的用户名为啥很少见中文?
String filein = "\r\n--你好;";
filein = new String(filein.getBytes(), "gb2312");gb2312,GBK,UTF-8
按需要选择
String filein = "abcdefg\r\n--你好;";
你会发现,所谓的乱码出现在abcdef前面。BOM头,你可以google上查一下。
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
此时你应该能明白了吧?
用UE看,是00 12两个字符,目前还不知道是什么。
其实,用BufferedWriter也挺好的。
byte[]a=filein.getBytes();
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename, false);
// mm.writeUTF(filein);
mm.write(a);
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方法,首先,写入两个字节,这两个字节代表长度,表明本次方法调用将实际写入多少字节的数据。
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();
}
}
}}}