Java opening File Streams in one class and closing/deletion of file in another class -
i want delete file opened , done writing not closed. please refer code below:
class (can't changed):
import java.io.fileoutputstream; public class { public void run(string file) throws exception { fileoutputstream s = new fileoutputstream(file); } } class b:
import java.io.file; import java.nio.file.files; import java.nio.file.paths; public class b { public static void main(string[] args) throws exception { string path = "d:\\conflux_home\\testclient\\maps\\test\\newtest.txt"; a = new a(); a.run(path); file f = new file(path); files.delete(paths.get(f.getabsolutepath())); } } in class , open stream without closing file. in class b , calling a's run method , try delete file. since file still opened. i'm unable delete file.
error : process cannot access file because being used process.
actual scenario : loading jars dynamically. classes inside jar creating file. when there exception, file gets created size 0 bytes. need delete file. since file not closed during exception, can't delete file.
we fix issue if close streams in jar classes, can't modify jars create files client specific jars.
please suggest how delete opened file, without modifying code in class a.
make sure close file, if there exception when writing it.
e.g.
public void run(string file) throws exception { fileoutputstream s = null; try { s = new fileoutputstream(file); } { try { s.close(); } catch(exception e) { // log exception } } }
Comments
Post a Comment