写文件时原内容不变,只是在每行的尾部添加一个字符即可。用RandomAccessFile的writeBytes会发生换行写,不能写到行尾的问题 请问哪位可以帮我提示提示

解决方案 »

  1.   

    我试验了,可以阿,他会覆盖最后的换行符,所以你需要追加
    import java.io.*;class  RandomAccess_Test
    {
    public static void main(String[] args) 
    {
    try{
    RandomAccessFile raf = new RandomAccessFile(new File("D:\\test\\aaa.java"),"rw");
    raf.seek(raf.readLine().length());
    raf.writeBytes("**"+"\t");
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
      

  2.   

    例如
    xxx1017
    xxx0022
    xxx0023
    xxx0025
    xxx1669执行后格式就乱了啊  下面的行会爬上上面来~
      

  3.   

    可以用StringBuffer的APPEND()和stringTokenizer试试看
      

  4.   

    楼主试试这个public static void main(String[] args) {
    try {
    RandomAccessFile raf = new RandomAccessFile(new File("D:\\1.java"),
    "rw");
    //raf.seek(raf.readLine().length());
    while(raf.readLine() != null)
    {

    }
    raf.writeBytes("**" + "\t");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  5.   


    public static void main(String[] args) {
    try {
    new testLog().appendChar('a');
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public void appendChar(char c) throws IOException {
    File inFile = new File("D:/temp/1.txt");
    File outFile = new File("D:/temp/2.txt");

    Reader ir = new FileReader(inFile);
    Writer ow = new FileWriter(outFile);

    BufferedReader br = new BufferedReader(ir);
    BufferedWriter bw = new BufferedWriter(ow);

    while(br.ready()) {
    String line = br.readLine();
    line += c;
    bw.write(line);
    bw.newLine();
    }

    br.close();
    bw.close();
    }
    1.txt1111
    2222
    333
    4444