我正在尝试使用以下代码访问文件以读取和写入文件:
RandomAccessFile file1 = new RandomAccessFile("C:\\lol.txt", "rw");
它返回一个错误“找不到文件(IOException)”。
该文件存在,并且位于该文件夹中。我错过了什么?
我正在尝试使用以下代码访问文件以读取和写入文件:
RandomAccessFile file1 = new RandomAccessFile("C:\\lol.txt", "rw");
它返回一个错误“找不到文件(IOException)”。
该文件存在,并且位于该文件夹中。我错过了什么?
Unless your run your Java application as an administrator, you won't have write access to C:
.
The following code
public static void main(String[] args) throws Exception {
RandomAccessFile file1 = new RandomAccessFile("C:\\lol.txt", "rw");
}
will give you
Exception in thread "main" java.io.FileNotFoundException: C:\lol.txt (Access is denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at Test.Main.main(Main.java:79)
The javadoc for RandomAccessFile
constructor states this:
FileNotFoundException - if the mode is "r" but the given string does not denote an existing regular file, or if the mode begins with "rw" but the given string 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
Just move your file to another location, like C:\Users\You
.