0

I have an object of class File that represent a directory. I now want to create a new File object that refers to a file within that directory. The following code seems to work:

String filePath = myDirectory.getAbsoutePath().concat("\\myFileName");
File theFile = File(filePath);

But, I was wondering if there was a way to do it without the File --> String --> File conversion.

Thanks.

1 Answer 1

2

Use the constructor public File(File parent, String child)

like File newFile = new File(myDirectory, "myFileName");

See for example: http://www.java2s.com/Tutorials/Java/java.io/File/Java_File_File_parent_String_child_Constructor.htm

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

1 Comment

That was easy. Thanks.

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.