Gandalf StormCrow
Gandalf StormCrow

Reputation: 26192

Trying to copy file from one location to another

I'm trying to copy a 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.

Upvotes: 2

Views: 1141

Answers (4)

foret
foret

Reputation: 728

If you write the 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.

Upvotes: 4

ozk
ozk

Reputation: 2022

First, it seems you forgot to close the parenthesis containing the method's arguments.

Second, are you sure getOutputFile() yields a complete file?

Upvotes: 1

Paul Jowett
Paul Jowett

Reputation: 6581

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).

Upvotes: 1

Zak
Zak

Reputation: 7078

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

Upvotes: 1

Related Questions