下面是我做的一个方法,可为什么老抛出异常,并且不能加到文本文件末尾??
到底这个方法错在哪?
static void addindic(String s){
   String s1=null;
  try{FileInputStream rf1=new FileInputStream("dictionary.txt");
          int n=512;
          byte buffer[]=new byte[n];
          while((rf1.read(buffer,0,n)!=-1)&&(n>0))
          {s1=new String(buffer);
           rf1.close();
          }
     PrintWriter a=new PrintWriter(new FileOutputStream("dictionary.txt"));
     a.write(s,s1.length(),s.length());
   } catch(Exception e)
          {System.out.println(e);}
   }

解决方案 »

  1.   

    可能我弄错了,那怎么才能将一个字符串加到一个.txt文件末尾呢?不能形成覆盖
      

  2.   

    PrintWriter out = new PrintWriter(new FileOutputStream("d:/target.txt",true),true);
    out.println("add");
      

  3.   

    是PrintWriter的一个方法。
    void println(String x)   打印 String,然后终止该行。
      

  4.   

    String x = "hello World";
    PrintWriter pw = new PrintWriter(new FileWriter("ok.txt",true),true);
    pw.append(s);//FileWriter和FileOutputStream第二個參數要true.沒有true的話會打開一個空文件。
      

  5.   

    API文档上讲得清清楚楚啦...public FileOutputStream(File file,
                            boolean append)
                     throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。 
      

  6.   

    package cn.manager;
    /**
     * @version 0.01
     * @author zhanshengkui
     * 目标
     */
    import java.io.*;
    public class TestKeyIn { /**
     * @param args
     */
    //读取键盘的一行数据
    //写入文件;
    public static void readLineFile(String toFile){
    //带缓存的字符流
    //不可直接使用
    //System.in表示标准键盘输入
    BufferedReader buff = new BufferedReader(
      new InputStreamReader(System.in));
    RandomAccessFile ra = null;
    try{
    ra = new RandomAccessFile(toFile,"rw");
    //
    ra.seek(ra.length());//移动文件读写指针 
    String line = buff.readLine();
    while(!line.equalsIgnoreCase("quit")){
    //ra.writeUTF(line);
    ra.write(line.getBytes("GBK"));
    ra.write("\r\n".getBytes("GBK"));
    line = buff.readLine();
    }
    }catch(FileNotFoundException e){
    e.printStackTrace();
    }catch(IOException el){
    el.printStackTrace();
    }finally{
    try{
    buff.close();
    ra.close();

    }catch(FileNotFoundException eo){
    eo.printStackTrace();
    }catch(IOException ei){
    ei.printStackTrace();
    }
    }
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    readLineFile("c://a.txt");
    }}
      

  7.   

    还是这个好些;
    ------------------
     jk88811(jkzqw) ( ) 信誉:100    Blog  2006-11-25 22:38:46  得分: 0  
     
     
       
    API文档上讲得清清楚楚啦...public FileOutputStream(File file,
                            boolean append)
                     throws FileNotFoundException创建一个向指定 File 对象表示的文件中写入数据的文件输出流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 file 参数表示的路径作为参数来调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。   
     
      

  8.   

    用RandomAccessFile追加写入阿
    File file = new File("***");
    RandomAccessFile raf = new RandomAccessFile (file);
    raf.seek(file.length);
    //再把字符串写入
    .....