Consider the code:
File file = new File("c:\\temp\\java\\testfile");
testfile is a file, and it may or may not exist.
I want to get the directory c:\\temp\\java\\ using the File object. How do I go about doing this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In either case, I’d expect
file.getParent()(orfile.getParentFile()) to give you what you want.Additionally, if you want to find out whether the original
Filedoes exist and is a directory, thenexists()andisDirectory()are what you’re after.