本帖最后由 suexiukjung 于 2011-09-28 17:51:44 编辑

解决方案 »

  1.   

    DefaultEditorKit kit = new DefaultEditorKit();
    Document doc = ta.getDocument();
    kit.write(new FileWriter(..), doc,0,doc.getLength());
      

  2.   

    我是用RandomAccessFile:RandomAccessFile raf = new RandomAccessFile(file, "rw");
    raf.seek(raf.length());
    raf.write((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+message+"\r\n").getBytes("UTF-8"));
    raf.close();
      

  3.   

    还有,在创建文件前先判断下文件是否存在:file = new File(fileName);
    if (!file.exists()) {
        try{
            file.createNewFile();
        }catch(Exception e){
            throw new AppException("创建日志文件错误!文件名:"+fileName);
        }
    }
      

  4.   

    用RandomAccessFile和append()都没有成功,能说的稍微详细点吗??
      

  5.   

    不清楚楼主是什么问题,难道就是因为向文件中追加内容不成功(只能显示一行)?
    参考下,可以追加写入文件:import java.io.*;public class Write2File {
    public static void main(String[] args) {
    String filename = "xxx.txt";
    String textInTextArea = "Hello TextArea",
       textInTextField = "Hello TextField";
    BufferedWriter bw = null;
    StringBuilder sb = new StringBuilder(1024);
    try {
    bw = new BufferedWriter(new FileWriter(filename, true), 1024);
    for (int i = 0; i < 5; ++i) {
    sb.append(textInTextArea).append(", ").append(textInTextField);
    bw.write(sb.toString(), 0, sb.length());
    bw.newLine();
    sb.delete(0, sb.length());
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (bw != null) {
    try {
    bw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }运行后查看xxx.txt文件内容:
    $ cat xxx.txt 
    Hello TextArea, Hello TextField
    Hello TextArea, Hello TextField
    Hello TextArea, Hello TextField
    Hello TextArea, Hello TextField
    Hello TextArea, Hello TextField若真是这个的话,那楼主就要补补JAVA中的IO操作了。
      

  6.   

    谢谢楼上几位的建议,最后我用了stringbuffer 和append()解决了。在左边我又加了个“写入”按钮和该按钮的事件处理。