2013-05-06 93 views
0

我正在使用以下代码在文件中存储一些数据。 (MYDATA是用户输入(双列表)和dates_Strings数据是我存储日期字符串列表)保存包含日期的文件仅保留最后的值

public void savefunc(){ 

     SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy",Locale.US); 
     Date d=new Date(); 

     String formattedDate=thedate.format(d); 
     Log.d("tag","format"+formattedDate); 
     dates_Strings.add(formattedDate); 



      double thedata=Double.parseDouble(value.getText().toString().trim()); 
      mydata.add(thedata); 


     File sdCard = Environment.getExternalStorageDirectory(); 
     File directory = new File (sdCard, "MyFiles"); 
     directory.mkdirs();    
     File file = new File(directory, filename); 

     FileOutputStream fos; 


     try { 
      fos = new FileOutputStream(file); 

       BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); 
       for (int i=0;i<mydata.size();i++){ 
        bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n"); 
       } 
       value.setText(""); 
       bw.flush(); 
       bw.close(); 

      } catch (IOException e2) { 
       e2.printStackTrace(); 
       } 
    } 

的问题是,如果我进入13年6月5日的一些数据,后来一些数据在07/05/13,该文件只包含最后一次数据的最后一个数据。我想保留所有数据。

回答

0

开启在附加模式的FileOutputStream中

fos = new FileOutputStream(file, true);