gpt4 book ai didi

java - 在 Android 中保存文件

转载 作者:行者123 更新时间:2023-12-01 18:45:22 24 4
gpt4 key购买 nike

我有以下代码:

    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) 在将数据写入文件之前,请确保该数据尚不存在。如果确实存在,则提示用户查看是否可以覆盖。如果用户选择"is",则覆盖该文件,但如果用户选择“否”,则在文件名末尾添加“1”并保存。 (无法工作并需要帮助)

我收到以下行的错误:

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

错误:

Unhandled exception type FileNotFoundException

--> (Surround with try/catch)

最佳答案

1)

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

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

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

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

关于java - 在 Android 中保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com