java - Apache commons-io FileUtils.deleteDirectory is not working properly -


i've problem commons-io fileutils.deletedirectory(file). call

fileutils.deletedirectory(new file("/tmp/dir")); 

directory structure is:

tmp/  - dir/     - a/        - b/           - c.txt 

i try debug following results:

  1. i stop program in fileutils before c.txt deleted. if (!file.delete())
  2. file present , can rename (it not locked guess then).
  3. file.delete() returns true, program continues normal way (the file still present, can't rename it)
  4. i stop program before b/ directory removed. if (!directory.delete())
  5. c.txt still present in directory , delete() on directory returns false , "unable delete directory /tmp/dir/a/b/" exception thrown
  6. when program ends file removed, b/, a/, dir/ directories not.

weird behavior me c.txt file exists after deletion , invoking delete on parent dir cause error then. file used java program. suggestions? idea how check in java if filehandlers still open file?

update: fixed

i'm stupid jerk, checked code again , found out miss close stream read file. had code reading input:

bytearrayoutputstream baos = new bytearrayoutputstream(); inputstream = new fileinputstream(new file("/tmp/dir/a/b/c.txt")); ioutils.copy(is, baos);  string content = new string(baos.tobytearray()); 

and change (it works now):

bytearrayoutputstream baos = new bytearrayoutputstream(); inputstream = new fileinputstream(new file("/tmp/dir/a/b/c.txt")); ioutils.copy(is, baos); // close streams! baos.flush(); baos.close(); is.close(); // important! string content = new string(baos.tobytearray()); 

this example, know important close streams correctly (using try-finally). bufferedinputstream should useful here too.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -