0

I am Creating a new File using RandomAccessFile with "rw" mode. but it gives

java.io.FileNotFoundException: ../dir/test.txt (No such file or directory)

reference

This is how i created:

File baseDirAsFile = new File("../");

File dirFile = new File(baseDirAsFile, "dir");

File file = new File(dirFile, "test.txt");

RandomAccessFile raf = new RandomAccessFile(file, "rw");

Note: It is not throwing that exception all the time. But can't identify when and why it is throwing this at some particular time.

Community
  • 1
  • 1

1 Answers1

0

I believe your code should be:

String baseDir = new File(".").getAbsolutePath();
String dirFile = baseDirAsFile + File.separator + "dir";
File file = new File(dirFile + File.separator + "test.txt");
RandomAccessFile file = new RandomAccessFile(file, "rw");
JD9999
  • 394
  • 1
  • 7
  • 18
  • when i get the path for dirFile it shows the absolutePath there is no difference between both – Suriya Vijayaraghavan Feb 22 '16 at 08:47
  • Hang on. You're using this to create a file. Why not use `file.createNewFile()` (java.io.File file) – JD9999 Feb 23 '16 at 04:30
  • There isn't supposed to be a difference between the two files. I have never tried using "rw" mode to create the file, and wouldn't. Just use the java.io.File.createNewFile method as above. – JD9999 Feb 23 '16 at 04:33