Android 文件写入只读文件系统警告

发布于 2025-01-04 05:31:45 字数 1755 浏览 0 评论 0原文

我正在尝试使用 FileOutputStreamOutputStreamWriter 写入文件,但我不断收到只读警告。

这是我正在使用的代码

FileOutputStream fw = new FileOutputStream((trackName.replaceAll(" ", ""))+".gpx", true);
    OutputStreamWriter osw = new OutputStreamWriter(fw);
    osw.write(XML_HEADER+"\n");
    osw.write(TAG_GPX+"\n");


    writeTrackPoints(trackName, osw, locations, time, trackDescp);
    writeWayPoints(osw, locations,time);
    osw.flush();
    osw.close();

,并且 Logcat 输出是

java.io.FileNotFoundException: /test.gpx (Read-only file system)
at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
at com.fyp.run_race.GPXFileWriter.<init>(GPXFileWriter.java:25)
at com.fyp.run_race.GPSTrackDetails$1.onClick(GPSTrackDetails.java:58)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

我确定问题到底是什么。

I'm trying to write a file using the FileOutputStreamand OutputStreamWriterbut I keep getting a read only warning.

Here is the code that I am using

FileOutputStream fw = new FileOutputStream((trackName.replaceAll(" ", ""))+".gpx", true);
    OutputStreamWriter osw = new OutputStreamWriter(fw);
    osw.write(XML_HEADER+"\n");
    osw.write(TAG_GPX+"\n");


    writeTrackPoints(trackName, osw, locations, time, trackDescp);
    writeWayPoints(osw, locations,time);
    osw.flush();
    osw.close();

And the Logcatoutput is

java.io.FileNotFoundException: /test.gpx (Read-only file system)
at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
at com.fyp.run_race.GPXFileWriter.<init>(GPXFileWriter.java:25)
at com.fyp.run_race.GPSTrackDetails$1.onClick(GPSTrackDetails.java:58)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

I ament sure what the problem is really.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑶笙 2025-01-11 05:31:45

不知道为什么普拉蒂克的评论被否决了——他是对的,因为你需要写入外部存储的权限。假设这就是你想要写的地方。

Devconsole 也是对的;您不能只写入任意文件名,大多数文件系统都被锁定且只读。

要写入外部存储(sdcard)中的任意位置,请使用 getExternalStorageDirectory() 获取外部存储的根目录。另请使用 getExternalStorageState() 确认外部存储存储实际上可用。

不要将文件放在外部存储的根目录下;将它们放在子目录中,以防止根目录变得过于混乱。 (编辑:有些 Android 实现无论如何也不会让你这样做。)

使用 getExternalFilesDir() 获取外部存储中名义上对您的应用程序私有的位置。 (API 8 中的新增功能)

使用 getFilesDir() 获取 /data/ 下可以写入应用程序私有文件的位置。这不在外部存储上,因此空间紧张;不要在这里写入大文件。

另请参阅:

  • getDir() -- 获取 /data/data/yourapname/ 下的目录
  • getFilesDir() -- 返回目录 /data/data/yourappname/files/
  • fileList() -- 列出 /data/data/yourappname/files/ 中的文件
  • getFileStreamPath() -- 获取 /data/data/yourappname/files/ 中的文件
  • openFileInput() -- 从 /data/data/yourappname/files/ 中打开文件以进行读取
  • openFileOutput() -- 从 /data/data/yourappname 中打开文件>/files/ 用于写入
  • getCacheDir() -- 返回目录 /data/data/yourappname/cache/
  • getExternalCacheDir() -- 返回目录/sdcard/Android/data/yourappname/cache/
  • deleteFile()

Dunno why Pratik's comment was downvoted -- he's right in that you'll want the permission to write to external storage. Assuming that's where you want to write.

Devconsole is also right; you can't just write to any arbitrary filename, most of the file system is locked down and read-only.

To write to an arbitrary location in external storage (sdcard), use getExternalStorageDirectory() for the root of external storage. Also use getExternalStorageState() to confirm that external storage is actually available.

Don't place files in the root of external storage; place them in a subdirectory to prevent the root from becoming too cluttered. (Edit: and some Android implementations won't let you do this anyway.)

Use getExternalFilesDir() to get a location in external storage that's nominally private to your application. (New in API 8)

Use getFilesDir() to get a location under /data/ where you can write application-private files. This isn't on external storage, so space is tight; don't write big files here.

See also:

  • getDir() -- Get a directory under /data/data/yourapname/
  • getFilesDir() -- Return the directory /data/data/yourappname/files/
  • fileList() -- List the files in /data/data/yourappname/files/
  • getFileStreamPath() -- Get a file in /data/data/yourappname/files/
  • openFileInput() -- Opens a file from within /data/data/yourappname/files/ for reading
  • openFileOutput() -- Opens a file from within /data/data/yourappname/files/ for writing
  • getCacheDir() -- Return the directory /data/data/yourappname/cache/
  • getExternalCacheDir() -- Return the directory /sdcard/Android/data/yourappname/cache/
  • deleteFile()
在梵高的星空下 2025-01-11 05:31:45

您只能将文件写入内部存储(设备内存)或外部存储(SD 卡),请参阅 http://developer.android.com/guide/topics/data/data-storage.html。简单地打开具有任意文件名的 FileOutputStream 是行不通的,因为该文件系统是只读的。

You can write files only to Internal Storage (device memory) or External Storage (the SD card), see http://developer.android.com/guide/topics/data/data-storage.html. Simply opening a FileOutputStream with an arbitrary file name won't work since that file system is read-only.

夏至、离别 2025-01-11 05:31:45

只需在清单中授予许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

just give permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文