I want to create a directory, enter that directory and copy files into it.
Is this possible in java?
I want to create a directory, enter that directory and copy files into it.
Share
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.
Creating a directory is possible. Use
File.mkdirorFile.mkdirs.Copying files into a directory is possible. There are many ways to do it, depending on what/where you are copying from.
By entering the directory, I assume that you mean make the new directory the JVM’s “current directory”. Unfortunately, standard Java doesn’t let you do this. There is no way to change the JVM’s current directory. However:
You don’t need to change directory do what what you are trying to do … copy files to a new directory.
In general, an application that needs a notion of a current directory (for the entire application, or for an individual thread), you can implement it by creating
Fileobjects relative to aFilethat represents the relevant current directory.I don’t know for sure, why the Java designers decided to not support “chdir” functionality. However:
Some OS platforms may not natively support changing the directory, leading to problems in supporting this functionality safely and efficiently in Java. (The JVM would need to do relative to absolute path mapping itself before passing the pathname to the OS. And there are likely to be race conditions to be avoided.)
The fact that “chdir” (as supported natively by UNIX / Linux for example) affects the entire process can make it problematic in a multi-threaded Java applications.