user1888243
user1888243

Reputation: 2691

Scala: Create File with a Folder Using Relative Path (Not Absolute Path)

In my Scala code I want to create a folder "c:/temp" and then create a file "file.txt" within that folder. I don't want to have to use "c:/temp/file.txt". So, I want to use the relative path of the file to create it within that folder.

Imagine how a human creates a folder and then a file? He creates a folder; goes in the folder, and then creates the file inside that folder. That's what I want to do.

=====

Added the following to make this more clear: Let's say I created the folder and I have a File object called myFolder that represents that folder. What I want is to be able to do something like myFolder.createFile("file.txt").

Upvotes: 0

Views: 1746

Answers (1)

jwvh
jwvh

Reputation: 51271

val subFile = new File(myFolder, "file.txt")

From the description of the File(File parent, String child) constructor found at the docs page:

Creates a new File instance from a parent abstract pathname and a child pathname string.

Upvotes: 1

Related Questions