So I am trying to read a file into a byte array using apache common library, and then delete the file itself. But I cannot do it. can someone let me know why?
File aFile = new File("path_To_A_PDF_File");
byte[] fileBytes = IOUtils.toByteArray(new FileInputStream(aFile));
boolean result = aFile.delete();
System.out.println("is file " + aFile.getAbsolutePath() + " deleted? " + result);
How to I delete that file? Thanks
您必须关闭通过以下方式打开的流:new FileInputStream(aFile)
The File
class contains a method to help with this. Call deleteOnExit()
and let the JVM clean it up for you later.
aFile.deleteOnExit();
Per the linked Javadoc,
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
I think you still have the FileInputSteam open on aFile. Try IOUtils.closeQuietly() on it (you'll need to make it a local variable instead of anonymous as you have it now).
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.