0

When we create a file using FileInputStream or RandomAccessFile classes, it's created in the same directory as the current project exists in (say \Project). What do I do if I wanna create the file in Project\dir1?? And also, how do I open the file from dir1??


EDIT (code and details added)

Creating a file in this manner

FileInputStream myFile=new FileInputStream("Filename.extension");
/* or */
RandomAccessFile RAF=new RandomAccessFile("Filename.extension", "rw");

creates the file in the directory of the current project. the file named Filename.extension gets created in the directory E:\JAVA1\Project but I wanna create the file in E:\JAVA1\Project\dir1. Can anyone please tell me how to do that? How do I specify the path to the sub-directory?

  • 3
    Possible duplicate of [How to create a file in a directory in java?](http://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java) – Matt Mar 10 '16 at 15:16

2 Answers2

0

Specify the relative path to your file :

InputStream stream = new FileInputStream("dir1/filename.txt");
Pazgabear
  • 93
  • 1
  • 6
0

When you create a file in Java you must specify either it's relative or it's absolute path, not just a file name. So if you create a file "file.txt" you are actually creating a file in current directory, and because it's relative path doesn't contain any slashes you may think that method accepts the file name, but you could have been more expressive and use "./file.txt" to achieve the same result, but making it clear with "./" that you are using current folder as a parent to the file.

Kudin
  • 456
  • 4
  • 7