Jicksy John
Jicksy John

Reputation: 169

How to copy a file to another location for later use (java)

I am using this code

JFileChooser jf=new JFileChooser();
File f=jf.getSelectedFile();
name=f.getName();

I get the name of the file that I have chosen. In the project that I am doing I need to access this attached file again. I couldn't copy the original path. I want to know if I can copy the file to another location (the contents of the file- file being a .txt file) after it is attached, so that I can access the same for later use.

Thank you.

Upvotes: 1

Views: 91

Answers (1)

Kick
Kick

Reputation: 4923

You can copy file using Files class

Files.copy(f.toPath(), dest.toPath());

or

You can use Apache Commons IO lib to copy

FileUtils.copyFile(File source, File dest);

Upvotes: 5

Related Questions