这是我写关于文件写入小程序
import java.io.*;
public class FileDemo {
   public void writeFile(){
     BufferedReader ds=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("enter string");
     String str=new String();
     try{
      str=ds.readLine();
     }
     catch(IOException e){
        e.getStackTrace();
      }
      try{
       String string=new String();
       string=new FileDemo().ReadFile();
       DataOutputStream o=new DataOutputStream(new FileOutputStream("d:/java/text.txt"));
       try{
         System.out.println(str);
         o.writeBytes(string+","+str+ "\r\n");
       }
        catch(IOException e2){
         System.out.println("文件写入失败");
        }
      }
      catch(FileNotFoundException e1){
       System.out.println("文件不存在");
       }
   }
   public static void readFile(){
    try{
      BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
      String str,string;
      try{
          str=d.readLine();
          System.out.println(str);
         }
      catch(IOException e){
        System.out.println("文件读出失败"); 
       }
    }
    catch(Exception e1){
      e1.getStackTrace();
    }
  
   }
   public String ReadFile(){
    String str=new String();
    try{
      BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("d:/java/text.txt")));
      try{
          str=d.readLine();
          System.out.println(str);
          
         }
      catch(IOException e){
        System.out.println("文件读出失败"); 
       }
    }
    catch(Exception e1){
      e1.getStackTrace();
    }
      return str; 
   }
    public static void main(String[] args) {
      new FileDemo().writeFile();
      readFile();
   }
}
上面的程序中基本解决了向文件写入新的字符串会覆盖原有的字符串的问题,但是如果字符串很长,会占有很多的内存空间,这个问题怎么解决呢? 如果有更完美的办法,请各位大侠将自己的原码一览,
谢谢,