\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();
}
}
} }}

解决方案 »

  1.   

    你什么系统下?不是widows吧?
      

  2.   

    是widows,JDK版本是jdk1.5.0_07,是不是JDK版本低啊
      

  3.   


    String filein = "\r\n--你好;";
    filein = new String(filein.getBytes(), "gb2312");gb2312,GBK,UTF-8
    按需要选择
      

  4.   

    是writeUTF()方法的问题,可能是writeUTF方法改变字符串编码时出的错吧,你换成writeBytes()就没有你说的问题了。
      

  5.   

    并不是\r\n为乱码,而是writeUTF在文件中写入了BOM头。不行你可以这样:
    String filein =  "abcdefg\r\n--你好;";
    你会发现,所谓的乱码出现在abcdef前面。BOM头,你可以google上查一下。 
      

  6.   

    首先注意看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
    此时你应该能明白了吧?
      

  7.   

    不好意思,我说得BOM头的问题有错。
    用UE看,是00 12两个字符,目前还不知道是什么。
    其实,用BufferedWriter也挺好的。
      

  8.   

    修改后片断如下: 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);
      

  9.   


    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方法,首先,写入两个字节,这两个字节代表长度,表明本次方法调用将实际写入多少字节的数据。
      

  10.   


    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();
    }
    }
    }}}