1

I have Java class that decrypt a file based on a user password. I have a demo.jar and a file content.txt that contain the encrypted content. The program runs and based on the user password decrypt the file into a file called content_recovered.txt

As long as I do this on my computer, it works great. I put the files on a flash drive and run it from there and it does not work. The file content_recovered.txt is created but empty !

Anybody has an idea on how to make it work from a flash drive ?

1 Answer 1

0

The only reason that I can think why this would not work is that the buffer size on a USB might be larger than on disk and you might not be flushing your output stream buffers properly (or closing your input file properly).

Check your io code to make sure that you are flushing and closing when writing, and making sure that there are no other file references open when reading:

OutputSteam os = null;

try {
   os = new BufferedOutputStream(new FileOutputStream(folder,"content.txt"));
   writeEncryptedFile(os); // do your file writing here
} catch (Exception e) {
   e.printStackTrace(); // whatever your error logging is here.
} finally {
   if (os != null) {
      // MUST ALWAYS FLUSH BEFORE CLOSING OUTPUTSTREAM
      try { os.flush(); } catch (Exception e) {}
      try { os.close(); } catch (Exception e) {}
   }
}

Also - make sure that you close your input files in the finally block too. This has caught me out on many occasions - not any more though :)

2
  • The files are properly closed so it might come from the buffer size. How can I fix that ? Because the buffer size is my code is the same.
    – user512684
    Nov 18, 2010 at 21:13
  • Just make sure you call flush() before close() and the buffer should not be an issue.
    – Chris
    Feb 25, 2011 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.