java.io.FileNotFoundException在内部存储android中保存/加载(java.io.FileNotFoundException Save/Load in internal

编程入门 行业动态 更新时间:2024-10-28 00:24:19
java.io.FileNotFoundException在内部存储android中保存/加载(java.io.FileNotFoundException Save/Load in internal storage android)

我正在尝试将数据从应用程序保存到内部存储并加载回来,但我觉得我错过了一些内容并且在加载中找不到该文件。

保存代码:

private void saveScanData(List<MyStack> surf) throws IOException { Log.i(TAG, "Saving"); String filename = String.format("Scan%05d.data", scanNumber); scanNumber += 1; ObjectOutput out; File outFile = new File(Environment.getExternalStorageDirectory(), filename); out = new ObjectOutputStream(new FileOutputStream(outFile)); out.writeObject(surf); out.close(); }

加载代码:

public void load(View view) { ObjectInput in; List<MyStack> surf= null; try { in = new ObjectInputStream(new FileInputStream("Scan%05d.data")); surf= (List<MyStack>) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} Model model= new Model(surf); Intent intent = new Intent(this, EditorPresenter.class); intent.putExtra("model", model); startActivity(intent); }

感谢您的任何帮助。

I'm trying to save data from the app to internal storage and load it back, but I feel like I miss something and the file is not found in the load.

Save code:

private void saveScanData(List<MyStack> surf) throws IOException { Log.i(TAG, "Saving"); String filename = String.format("Scan%05d.data", scanNumber); scanNumber += 1; ObjectOutput out; File outFile = new File(Environment.getExternalStorageDirectory(), filename); out = new ObjectOutputStream(new FileOutputStream(outFile)); out.writeObject(surf); out.close(); }

Load code:

public void load(View view) { ObjectInput in; List<MyStack> surf= null; try { in = new ObjectInputStream(new FileInputStream("Scan%05d.data")); surf= (List<MyStack>) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} Model model= new Model(surf); Intent intent = new Intent(this, EditorPresenter.class); intent.putExtra("model", model); startActivity(intent); }

Thank you for any help.

最满意答案

使用open openFileOutput和openfileInput方法而不是打开新的输入和输出流。

http://developer.android.com/intl/es/guide/topics/data/data-storage.html#filesInternal

此外,您正在尝试将文件保存在ExternalStorage文件夹中。 Environment.getExternalStorageDirectory() 。 在该文件夹中写入需要AndroidManifest上的Android权限

Use the open openFileOutput and openfileInput method instead of opening new Input and output stream.

http://developer.android.com/intl/es/guide/topics/data/data-storage.html#filesInternal

Moreover, you are trying to save the file in the ExternalStorage folder. Environment.getExternalStorageDirectory(). Writting in that folder requires Android permissions on the AndroidManifest

更多推荐

本文发布于:2023-07-20 20:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在内部   加载   FileNotFoundException   io   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!