import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;public class Test{
public static void main(String []args){
try{
Runtime.getRuntime().exec("cmd /c del test.txt");
File fileTest = new File("testFirst.txt");
RandomAccessFile raf = new RandomAccessFile(fileTest, "rw");
String str = "my First Line Be Wrote Into My File";
raf.writeUTF(str);
raf.seek(0);
while(raf.getFilePointer() < raf.length()){
System.out.println(raf.readLine());
}
raf.close();
Runtime.getRuntime().exec("cmd /c testFirst.txt");
}catch(IOException ioe){

}
}
}
//console
 #my First Line Be Wrote Into My File为什么会先输出一个空格和一个向上的箭头,然后才是我设定的my First Line Be Wrote Into My File呢????求助...能不能把这个空格和#去掉呢????感激

解决方案 »

  1.   


    如果你要写中文的话注意编码!
    import java.io.File;
    import java.io.RandomAccessFile;
    import java.io.IOException;public class Test {
    public static void main(String[] args) {
    try {
    Runtime.getRuntime().exec("cmd /c del test.txt");
    File fileTest = new File("testFirst.txt");
    RandomAccessFile raf = new RandomAccessFile(fileTest, "rw");
    String str = "my First Line Be Wrote Into My File";

    //raf.writeUTF(str);

    raf.write(str.getBytes());
    raf.seek(0);
    while (raf.getFilePointer() < raf.length()) {
    System.out.println(raf.readLine());
    }
    raf.close();
    Runtime.getRuntime().exec("cmd /c testFirst.txt");
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }
      

  2.   


    如果你要写中文的话注意编码!
    import java.io.File;
    import java.io.RandomAccessFile;
    import java.io.IOException;public class Test {
    public static void main(String[] args) {
    try {
    Runtime.getRuntime().exec("cmd /c del test.txt");
    File fileTest = new File("testFirst.txt");
    RandomAccessFile raf = new RandomAccessFile(fileTest, "rw");
    String str = "my First Line Be Wrote Into My File";

    //raf.writeUTF(str);

    raf.write(str.getBytes());
    raf.seek(0);
    while (raf.getFilePointer() < raf.length()) {
    System.out.println(raf.readLine());
    }
    raf.close();
    Runtime.getRuntime().exec("cmd /c testFirst.txt");
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }
      

  3.   

    多输出的这两个字符是因为writeUTF()方法
    以下是API中的定义public final void writeUTF(String str)
                        throws IOException
    使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。 
    首先,把两个字节从文件的当前文件指针写入到此文件,类似于使用 writeShort 方法并给定要跟随的字节数。此值是实际写出的字节数,而不是该字符串的长度。在该长度之后,按顺序输出该字符串的每个字符,并对每个字符使用 UTF-8 修改版编码。 多输出的这两个字符是占了两个字节的实际写出的字节数
      

  4.   

    只要使用了writeUTF()方法,不论写的是数字,字母,还是中文都会有这两个字节的多余输出。
      

  5.   

    package com.csdn;
    import java.io.File;
    import java.io.RandomAccessFile;
    import java.io.IOException;public class FileTest{
        public static void main(String []args){
            try{
                Runtime.getRuntime().exec("cmd/ c del test.txt");
                File fileTest = new File("testFirst.txt");
                RandomAccessFile raf = new RandomAccessFile(fileTest, "rw");
                String str ="my First Line Be Wrote Into My File";
                raf.writeUTF(str);
                raf.seek(0);
                while(raf.getFilePointer() < raf.length()){
                    System.out.println(raf.readUTF());//应该用raf.readUTF()来读,你刚用raf.readLine());来读的,
                    //与writeUTF(str)编码不一样
                }
                raf.close();
                Runtime.getRuntime().exec("cmd/ c testFirst.txt");
            }catch(IOException ioe){
                
            }
        }
    }