0

我有以下代码:

    btnSaveTrip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (showLog != null && showLog.getText().toString().length() > 0) {
                File folder = new File(Environment.getExternalStorageDirectory() + "/tc");
                if (!folder.exists()) {
                    folder.mkdir();
                }
                String externalStoragePath = Environment.getExternalStorageDirectory().toString();
                final File file = new File(externalStoragePath + "/tc/strip.tcl");
                try {
                    if (file.exists()) {
                        new AlertDialog.Builder(getActivity())
                        .setTitle("File Already Exist")
                        .setMessage("Do you want to overwrite the file?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                outputStream = new FileOutputStream(file);
                                outputStream.write(showLog.getText().toString().getBytes());
                                Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .show();
                    }
                    else {
                        outputStream = new FileOutputStream(file);
                        outputStream.write(showLog.getText().toString().getBytes());
                        Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText (getActivity(), "error in try", Toast.LENGTH_SHORT).show();
                }
                finally {
                    if(outputStream!=null) {
                        try {
                            outputStream.close();
                            Toast.makeText (getActivity(), "file closed", Toast.LENGTH_SHORT).show();
                        }
                        catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            Toast.makeText (getActivity(), "error in finally catch", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
            else {
                Toast.makeText (getActivity(), "empty", Toast.LENGTH_SHORT).show();
            }
        }
    });

我想做的是:

单击按钮时:

1)检查以使数据不为空或为空(工作正常):

if (showLog != null && showLog.getText().toString().length() > 0) {

2)检查以确保文件夹存在,如果不创建文件夹(工作正常):

            File folder = new File(Environment.getExternalStorageDirectory() + "/tc");
            if (!folder.exists()) {
                folder.mkdir();
            }

3)在将数据写入文件之前,确保它不存在。如果确实存在,则提示用户查看是否可以覆盖它。如果用户选择“是”,则覆盖文件,但如果用户选择“否”,则在文件名末尾添加“1”并保存。(不工作,需要帮​​助

我收到以下行的错误:

outputStream = new FileOutputStream(file);
outputStream.write(showLog.getText().toString().getBytes());

错误:

未处理的异常类型 FileNotFoundException

--> (用 try/catch 包围)

4

2 回答 2

3

1)

File tcDir = new File(Environment.getExternalStorageDirectory(),"tc");
tcDir.mkdirs();
File file = new File(tcdir, "strip.tcl");

第一行在外部存储目录中创建了一个名为 tc 的文件对象。第二行在磁盘上创建它,其中包含任何缺失的父项。第三行在该目录中创建一个文件对象

2)你似乎正在这样做 - 你创建一个文件输出流并像你正在做的那样写入它。

3)在写入文件之前,校准file.exists()。如果存在,则需要弹出一个AlertDialog。如果他们点击对话框的“是”按钮,您将编写一个文件。如果他们选择“否”按钮,您什么也不做。最好将所有编写代码放入一个单独的函数中,以便可以在对话框点击代码和此处的 !exists 代码中调用它。

于 2013-07-31T16:39:10.080 回答
1

在回答第 3 部分)时,我已编辑您的代码以使其正常工作。您拥有的大部分代码都是正确编写的,只有几个问题。我还移动了代码以将新文件写入新方法writeFile(),以避免复制代码并更容易跟踪正在发生的事情:

btnSaveTrip.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (showLog != null && showLog.getText().toString().length() > 0) {
            File folder = new File(Environment.getExternalStorageDirectory() + "/TollCulator");
            if (!folder.exists()) {
                folder.mkdir();
            }
            String externalStoragePath = Environment.getExternalStorageDirectory().toString();
            final File file = new File(externalStoragePath + "/TollCulator/strip.tcl");
            if (file.exists()) {
                new AlertDialog.Builder(getActivity())
                .setTitle("File Already Exist")
                .setMessage("Do you want to overwrite the existing file?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        writeFile(file);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
            } else {
                writeFile(file);
            }
        } else {
            Toast.makeText (getActivity(), "empty", Toast.LENGTH_SHORT).show();
        }
    }
});

// ...

private void writeFile(File file){
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(showLog.getText().toString().getBytes());
        Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(outputStream!=null) {
            try {
                outputStream.close();
                Toast.makeText (getActivity(), "file closed", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText (getActivity(), "error in finally catch", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
于 2013-07-31T18:07:52.530 回答