简体   繁体   中英

How to use File library io Java

How can I save a file from a directory location where I am also reading another file? What I mean is, I am reading a file at a certain directory ie

/Users/haddad/dir1/file.xls

I have a method which reads file.xls and I make a copy of it (I just copy the file and do an append to the name).

public void postProcessing(String fileName) throws Exception {

   // where fileName parameter is the absolute path to the original file.xls

   Workbook w = Workbook.getWorkbook(new File(fileName));
   WritableWorkbook copy = Workbook.createWorkbook(new File(fileName.replace(".xls", 
                                                         "_generated.xls")), w);

   some more processing...
}

My question is, how can I save this file at a different location because my current way it saves the file_generated.xls in the same path where it read the original file.

I'd like to have it saved here:

/Users/haddad/Desktop/file_generated.xls

You can use Apache Commons FileUtils class to copy file from one location to another:

FileUtils.copyFileToDirectory(srcFile, destDir);

This is a generic method for copying any type of file from one location to another. Both srcFile and destFile are instances of File class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM