2013-05-06 34 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); 
相關問題