Java Android - 仍然是旧文件

时间:2013-11-25 05:57:55

标签: java android file

我仍然收到第一个为我生成的应用程序文件。 首先,我认为这是因为文件存在所以我写了

File file=new File(getCacheDir(), "Competition.xls");
    if (file.exists()) {file.delete(); file =new File(getCacheDir(), "Competition.xls");}

但这对我没有帮助 - 我仍然收到第一个被制作的文件

我刚开始使用文件,所以我决定在这里复制整个方法。很抱歉有很多文字。

private void createFileTosend() {
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        File toSend=null;
        try {
            toSend = getFile();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        inputStream = new FileInputStream(toSend);


        outputStream = openFileOutput("Competition.xls",
                Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
        byte[] buffer = new byte[1024];
        int length = 0;
        try {
            while ((length = inputStream.read(buffer)) > 0){
                outputStream.write(buffer, 0, length);
            }
        } catch (IOException ioe) {
            /* ignore */
        }
    } catch (FileNotFoundException fnfe) {
        /* ignore */
    } finally {
        try {
            inputStream.close();
        } catch (IOException ioe) {
           /* ignore */
        }
        try {
            outputStream.close();
        } catch (IOException ioe) {
           /* ignore */
        }
    }
}
public File getFile()  throws IOException, WriteException{
    File file=new File(getCacheDir(), "Competition.xls");
    if (file.exists()) {file.delete(); file =new File(getCacheDir(), "Competition.xls");}

    WritableWorkbook  workbook = Workbook.createWorkbook(file); 
    //then goes long block with creating a .xls file which is not important
    workbook.write();
    workbook.close(); 

    return file;
}

帮助理解问题所在

1 个答案:

答案 0 :(得分:2)

你永远不应该有这样的结构:

catch(Exception ex ) {
     //ignore (or log only)
}

有例外可以告诉你出了什么问题。你做的是(法语)“吃/隐藏异常”。你正在丢失这些非常重要的信息,说明事情发生了异常。

你应该始终将你捕到的异常抛给你的调用者,或者在本地处理它。至少,这是一个糟糕的做法,你应该记录它。但什么都不做是非常错误的。

这里,将整个try catch放在一个方法中,例如:

private void createFileTosend() throws IOException {
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        File toSend = getFile();
        inputStream = new FileInputStream(toSend);


        outputStream = openFileOutput("Competition.xls",
                Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) > 0){
             outputStream.write(buffer, 0, length);
         }
    } finally {

        try {
            if( inputStream != null ) {
                inputStream.close();
            }
        } catch (IOException ioe) {
           Log.e( ioe );
        }
        try {
            if( outputStream != null ) {
                outputStream.close();
            }
        } catch (IOException ioe) {
           Log.e( ioe );
        }
    }
}

现在,当您调用createFileToSend时,请在try / catch结构中执行此操作并烘烤消息,或者如果您发现异常,则执行此操作。