Android没有写文件,或文件被以某种方式删除?

时间:2015-05-18 19:56:26

标签: java android

尝试编写一个xls文件,但它要么不会写入,要么一直被删除。系统报告正在删除某些类型的文件,但我真的不确定。我正在使用apache poi编写xls文件,并且在常规Java应用程序中使用它时没有任何问题。

任何帮助都将不胜感激。

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

1 个答案:

答案 0 :(得分:0)

你可以试着用这个替换你的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();

        }