看javadoc中java.io部分,写得很清楚,对你有好处。

解决方案 »

  1.   

    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.text.*;        InetAddress addresses = InetAddress.getLocalHost();
            StringBuffer sb_file = new StringBuffer(pathstr);
            sb_file.append("test");文件名称
            sb_file.append(".txt");
            FileOutputStream fos = new FileOutputStream(sb_file.toString(), true);
            StringBuffer sb_context = new StringBuffer("\r\n");
            sb_context.append("写注册表");
            sb_context.append("; ");
            sb_context.append(";\r\n");
      

  2.   


    追加数据到文件:
    package com;                                            
    import java.io.*;                                       
                                                            
    public class WriteAppend {                              
                                                            
    private String path;//文件路径                          
    private String something;//追加的字符串变量             
    //初始化                                                
    public WriteAppend() {                                  
    path = null;                                            
    something = "Default message";                          
     }                                                      
                                                            
     //设置文件路径                                         
     public void setPath(String apath) {                    
     path = apath;                                          
     }                                                      
                                                            
     //得到文件路径                                         
     public String getPath() {                              
     return path;                                           
     }                                                      
                                                            
     //设置要追加的字符串                                   
     public void setSomething(String asomething) {          
     something = asomething;                                
     }                                                      
                                                            
     //得到要追加的字符串                                   
     public String getSomething() {                         
     return something;                                      
     }                                                      
                                                            
     //追加字符串                                           
     public String writeSomething() {                       
     try {                                                  
     //创建文件path并写入something字符串,注意和写入篇的区别
     FileWriter theFile = new FileWriter(path,true);        
     PrintWriter out = new PrintWriter(theFile);            
     out.print(something + "\n");                           
     out.close();                                           
     //关闭文件并返回success字符串                          
     theFile.close();                                       
     return "success!!";                                    
     } catch (IOException e) {                              
     return e.toString();                                   
     }                                                      
     }                                                      
     }                                                      
      

  3.   

    要在c:\建立writefile.txt.追加数据到文件。WriteAppend.jsp
    <%@page contentType="text/html;charset=gb2312"%>                                   
    <html>                                                                             
    <head>                                                                             
    <title>追加数据</title>                                                            
    </head>                                                                            
    <body>                                                                             
    <%--创建javabean并设置属性 --%>                                                    
    <jsp:useBean id="writer" class="com.WriteAppend" scope="request">                  
    <jsp:setProperty name="writer" property="path" value="c:\\writefile.txt" />        
     <jsp:setProperty name="writer" property="something" value="初始化something属性" />
     </jsp:useBean>                                                                    
     <h3>追加数据</h3>                                                                 
     <p>                                                                               
     <%--设置要追加的字符串 --%>                                                       
     <% writer.setSomething("这是追加的内容"); %>                                      
     <%--读取上面设置的字符串 --%>                                                     
     <% out.print(writer.getSomething()); %>                                           
     <%--调用writer的writeSomething方法追加文件并返回成功或者出错信息 --%>             
     <% out.print(writer.writeSomething()); %>                                         
     </p>                                                                              
     </body>                                                                           
     </html>