使用 RandomAccessFile 创建文件时出现 java.io.FileNotFoundException

发布于 2024-09-16 04:39:04 字数 193 浏览 11 评论 0原文

当我尝试使用 RandomAccessFile 创建文件时遇到 FileNotFoundException:

RandomAccessFile file = new RandomAccessFile("/test.jpg", "rw");

我现在不知道如何解决这个问题。这让我抓狂。

谢谢

I'm encountering a FileNotFoundException when I try to make a file using RandomAccessFile:

RandomAccessFile file = new RandomAccessFile("/test.jpg", "rw");

I don't now how to get around this. It's driving me nuts.

Thanks

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

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

发布评论

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

评论(3

菩提树下叶撕阳。 2024-09-23 04:39:04

尝试

RandomAccessFile file = new RandomAccessFile(new File(getFilesDir(), "test.jpg"),
        "rw");

Try

RandomAccessFile file = new RandomAccessFile(new File(getFilesDir(), "test.jpg"),
        "rw");
我家小可爱 2024-09-23 04:39:04

从文档中:

FileNotFoundException - 如果模式为“r”但给定的文件对象不表示现有的常规文件,或者模式以“rw”开头但给定的文件对象不表示现有的、可写的常规文件且无法创建该名称的新常规文件,或者打开或创建文件时发生其他错误

您是否可以通过其他方式创建这样的文件?您是否在“/”表示根目录的环境中工作?

From the documentation:

FileNotFoundException - if the mode is "r" but the given file object does not denote an existing regular file, or if the mode begins with "rw" but the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file

Are you able to create such a file by other means? Are you working in an environment where "/" denotes the root directory?

迎风吟唱 2024-09-23 04:39:04

实际上,当我们只给出文件名时,就会发生此错误,

String fileName="Shiva.txt"
String Directory = Environment.getExternalStorageDirectory() + File.separator + "OneSecondMoments" + File.seseparator + fileName

例如:

RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");

给出整个目录的路径,例如

RandomAccessFile randomAccessFile = new RandomAccessFile(Directory, "rw");

Actually this error occurs when we only give the file name

String fileName="Shiva.txt"
String Directory = Environment.getExternalStorageDirectory() + File.separator + "OneSecondMoments" + File.seseparator + fileName

for example:

RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");

Give the Path of whole Directory, for example

RandomAccessFile randomAccessFile = new RandomAccessFile(Directory, "rw");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文