2015-05-18 22 views
0

试图编写一个xls文件,但它不会写或不断删除。系统报告某些类型的文件正在被删除,但我真的不确定。我正在使用Apache poi来编写xls文件,并且在使用常规Java应用程序时没有任何问题。Android不写文件,或者该文件以某种方式被删除?

任何帮助,将不胜感激。

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Workbook; 

import java.io.File; 
import java.io.FileOutputStream; 

public class Excel_Constructor extends Activity { 



public void main(String[] args) { 

    FileOutputStream fos = null; 


    Workbook workbook = new HSSFWorkbook(); 

    try { 

     fos =openFileOutput("Test.xls", this.MODE_PRIVATE); 
     workbook.write(fos); 
     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Toast.makeText(this, "There was a problem making the file", Toast.LENGTH_SHORT).show(); 
    } 
} 


public void saveData() { 

的logcat:

05-18 15:46:38.199  803-829/system_process I/ActivityManager﹕ Displayed 
com.example.liam.quickreport20/.Setup: +1s502ms 
05-18 15:46:40.329  803-925/system_process D/TaskPersister﹕ 
removeObsoleteFile: deleting file=235_task.xml 
05-18 15:46:40.330  803-925/system_process D/TaskPersister﹕ 
removeObsoleteFile: deleting file=235_task_thumbnail.png 
05-18 15:47:58.058  803-803/system_process I/ActivityManager﹕ Killing 
1608:com.android.externalstorage/u0a6 (adj 15): empty for 1817s 
05-18 15:47:58.082  803-1211/system_process W/libprocessgroup﹕ failed to 
open /acct/uid_10006/pid_1608/cgroup.procs: No such file or directory 
05-18 15:49:31.729  82-82/? D/Genyd﹕ Received Set Clipboard 
05-18 15:49:31.729  82-82/? D/Genymotion﹕ Received Set Clipboard 
05-18 15:50:05.405  82-82/? D/Genyd﹕ Received Set Clipboard 
+0

如果文件不存在,也不需要创建它首先使用“新文件(”Test.xls“)。createNewFile();”? – Guardanis

回答

0

你可以尝试replaceyour的try-catch这一个块:

FileOutputStream fos = null; 
     try { 

      String str_path = Environment.getExternalStorageDirectory().toString(); 
      File file ; 
      file = new File(str_path, "Test.xls"); 
      fos = new FileOutputStream(file); 


      workbook.write(fos); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (fos != null) { 
       try { 
        fos.flush(); 
        fos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      Toast.makeText(MainActivity.this, "Excel Sheet Generated", Toast.LENGTH_SHORT).show(); 

     } 
相关问题