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:
- i stop program in fileutils before c.txt deleted.
if (!file.delete())
- file present , can rename (it not locked guess then).
file.delete()
returnstrue
, program continues normal way (the file still present, can't rename it)- i stop program before b/ directory removed.
if (!directory.delete())
- c.txt still present in directory ,
delete()
on directory returns false , "unable delete directory /tmp/dir/a/b/" exception thrown - 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
Post a Comment