我有以下代码:
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)在将数据写入文件之前,请确保它尚不存在。如果它确实存在,则提示用户查看是否可以覆盖它。如果用户选择YES,则覆盖该文件,但如果用户选择NO,则在文件名末尾添加“1”并保存。 (不工作,需要帮助)
我收到以下行的错误:
outputStream = new FileOutputStream(file);
outputStream.write(showLog.getText().toString().getBytes());
错误:
未处理的异常类型FileNotFoundException
- > (用try / catch环绕)
答案 0 :(得分:3)
1)
File tcDir = new File(Environment.getExternalStorageDirectory(),"tc");
tcDir.mkdirs();
File file = new File(tcdir, "strip.tcl");
第一行在外部存储器directoy中创建一个名为tc的文件对象。第二行在磁盘上创建它,任何缺少的父项。第三行在该目录中创建一个文件对象
2)你似乎正在这样做 - 你创建了一个文件输出流并像你一样写信给它。
3)在写入文件之前,请先调用file.exists()。如果存在,则需要弹出AlertDialog。如果他们点击对话框的“是”按钮,则会编写一个文件。如果他们选择no按钮,则不执行任何操作。最好将所有编写代码放入一个单独的函数中,以便可以在对话框单击代码和!exists代码中调用它。
答案 1 :(得分: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();
}
}
}
}