如题。如何在已有文件的文件尾的下一行添加一行字符串

解决方案 »

  1.   

    有一个想法不知道是否可行(因为没有试过),首先把所有的内容读到内存中(Buffer..string),然后在这个bs里边加字符,最后再写到文件中去.
      

  2.   

    FileWriter
    public FileWriter(File file,
                      boolean append)
               throws IOException在给出 File 对象的情况下构造一个 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。 参数:
    file - 要写入数据的 File 对象
    append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处 
    抛出: 
    IOException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
    从以下版本开始: 
    1.4 
      

  3.   

    package test;
    import java.io.*;public class Append { /**
     * @param args
     * @throws IOException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    InputStreamReader isr = null;
    BufferedReader br = null;
    OutputStreamWriter osw = null;
    BufferedWriter pw = null;
    String line = null;
    int count = 0;
    try {
    isr = new FileReader("H:\\sn.txt");
    br = new BufferedReader(isr);

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    try {
    osw = new FileWriter("H:\\sn.txt",true);
    pw = new BufferedWriter(osw);
    pw.write("str");
    pw.flush();
    pw.newLine();
    } catch (IOException e1) {
    e1.printStackTrace();
    }finally{
    pw.close();
    osw.close();
    br.close();
    isr.close();
    }
    }
    }
      

  4.   

    根据楼主说的改了该
    public class Append { /**
     * @param args
     * @throws IOException 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    OutputStreamWriter osw = null;
    BufferedWriter pw = null;
    try {
    osw = new FileWriter("H:\\sn.txt",true);//this file is already exist.
    pw = new BufferedWriter(osw);
    pw.write("str");//append this string "str" to the file sn.txt.
    pw.flush();
    pw.newLine();
    } catch (IOException e1) {
    e1.printStackTrace();
    }finally{
    pw.close();
    osw.close();
    }
    }
    }
      

  5.   

    嗬嗬,又是你呀!字符串前面?
    不是后面吗?
    假如你给windows的文件加的话,用“\r\n”比较好。符合windows风格。赫赫。
      

  6.   

    AWUSOFT的读入内存的想法,近似疯狂。这样对服务器来说,简直就是一种折磨。文件大一些,连续操作的话,用不了多久,就OutOfMemory了。
      

  7.   

    BufferedWriter类,支持newline方法的。
    也可以做到的.
      

  8.   

    FileWriter f=new FileWriter(fileName,append);
    fileName - 一个字符串,表示与系统有关的文件名。
    append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。 
      

  9.   

    InputStreamReader isr=new InputStreamReader("xx.txt");
    BufferedReader br=new BufferedReader(isr);
    OutputStreamReader osr=new OutputStreamReader(br);
    for(int i=0;i<br.readln();i++){
      br.write(i);
    }
      

  10.   

    上面的写错了哈,不好意思  
    File f=new File("xx.txt"); 
    FileOutputStream fos=new FileOutputStream(f);
    OutputStreamReader osr=new OutputStreamReader(br); 
    String s="hello world";
    for(int i=0;i <s.length;i++){ 
      osr.write(i); 
    }
    osr.flush();
    osr.close();
    fos.close();
      

  11.   

    http://hi.baidu.com/johnsoncr/blog/item/31bbe73882bdf92097ddd8ce.html
    这里面介绍了三种方法