2013-11-15 77 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