Greg Valvo
Greg Valvo

Reputation: 1099

Concatenate String to End of File Object

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.

Upvotes: 0

Views: 89

Answers (1)

JFPicard
JFPicard

Reputation: 5168

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

Upvotes: 2

Related Questions