简体   繁体   中英

Trying to copy file from one location to another

Hello I'm trying to copy file to another directory with commons fileUtils. I tried this

FileUtils.copyFile(getOutputFile(), new File("RESULT/final_result.txt");

The new final_result.txt file contains only the first line of my output file, what did I do wrong?

Is there an alternative to commons IO, or some other way I'll take any as long as it does the trick.

if you write file (which you get by getOutputFile()) before this operation, be sure to flush() all changes.
Otherwise it seems to be a bug. But it is unlikely.

first, it seems you forgot to close the parenthesis containing the method's arguments. second, are you sure getOutputFile() yields a complete file?

Try new File(Result,"final_result.txt");
Result should be of type File and final_result.txt String

Perhaps you need to do a simple test using a debug or sleep:

  1. manually delete RESULT/final_result.txt
  2. run the code that creates your output file and either sleep, or use a breakpoint with debugging to stop after the files is created.
  3. Manually open RESULT/final_result.txt and see what's there.
  4. let your program finish it's task.

You'll either find that your write is not complete (in step 3) and you'll need to flush/close the correct ouput stream, or you'll find that the copy is doing something weird (which is less likely).

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