0

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").

7
  • I want to create a folder "/temp" : Do you want to create this directory in the root folder of the system / ? Commented Nov 3, 2018 at 20:09
  • Yes, the path to the folder is absolute path. Commented Nov 3, 2018 at 20:12
  • 1
    Files.createDirectory(Paths.get("/temp")) provided that you have root priviledges. Commented Nov 3, 2018 at 20:15
  • Sorry, I didn't explain clearly. I don't care about the root part; I don't worry about the permission. I changed the question to make it more clear. Commented Nov 3, 2018 at 20:19
  • 1
    It is not more clear ;) You can use the Java instruction in my previous comment to create a directory from a string. After folder creation. You can use the path temp to write files inside the newly created directory. Commented Nov 3, 2018 at 20:21

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

2 Comments

The OP is asking if they can do: myFolder.createFile("file.txt") with myFolder being some object representing the destination folder.
This is a correct answer to this question, please ignore the comment from @jrook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.