2013-11-15 78 views
0

文件中存儲多個值,我想我的值存儲爲文本文件裏,因此我用中以表格的形式

try 
     { 
      File Mydir = new File("/sdcard/app/"); 
      Mydir.mkdirs(); 
      File outputFile = new File(Mydir, "helloworld"); 
      FileOutputStream fos = new FileOutputStream(outputFile); 
     fos.write(item1.getBytes()); 
     fos.write(item2.getBytes()); 

     // Close output stream 
     fos.flush(); 
     fos.close(); 

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

這是工作非常好,和值存儲正確。但現在我的問題是我想存儲大量的值在一個單一的文件,我想存儲這些值的表格格式。這裏的值正在越過寫入的上面,最後使用的值只顯示。所以,而不是我想要以表格格式存儲所有使用的值。

+0

爲什麼寫這篇文章以表格的形式是非常重要的,如果它是所以importatant,寫一個CSV或任何其他這樣的格式 –

+0

我想存儲在另一個 – AndroidOptimist

+0

是一個表中的所有值,它只是你喜歡它的方式,或者有一些要求強迫呢? –

回答

0

我不明白以表格的形式書寫的要求,但如果你只是想新的文本不會覆蓋文件中現有的文本,或簡單的附加在文件中的新文本,你應該使用

FileOutputStream fos = new FileOutputStream(outputFile,true); 

代替

FileOutputStream fos = new FileOutputStream(outputFile); 

try 
     { 
      File Mydir = new File("/sdcard/app/"); 
      Mydir.mkdirs(); 
      File outputFile = new File(Mydir, "helloworld"); 
      FileOutputStream fos = new FileOutputStream(outputFile, true); 
      fos.write(item1.getBytes()); 
      fos.write(item2.getBytes()); 

      // Close output stream 
      fos.flush(); 
      fos.close(); 

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

編輯

對於當前系統日期和時間

SimpleDateFormat ft =new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss:S a zzz"); 
Date date= new Date(); 
String nowTime = ft.format(date); 

串連nowTimeitem1item2寫它的文件之前

+0

雅這是工作,但我可以存儲相同的表在一個列中的item1和列中的item2中的表? – AndroidOptimist

+0

@Alliswell在SO上看到這個答案http://stackoverflow.com/questions/740157/storing-data-in-txt-file-in-specific-format –

+0

可以告訴我如何添加系統當前的日期和時間上面的代碼。 – AndroidOptimist