i reading xml file, process elements , write xml file elements extracted form input file. using stax cursor: read elements xmlstreamreader , write elements in file using xmlstreamwriter.
the reader:
xmlinputfactory inputfactory = xmlinputfactory.newinstance(); xmlstreamreader streamreader = inputfactory.createxmlstreamreader(new filereader(inputfile)); the writer:
xmloutputfactory outputfactory = xmloutputfactory.newinstance(); xmlstreamwriter streamwriter = outputfactory.createxmlstreamwriter(new fileoutputstream (outputfile),"utf-8"); after finish processing , writing output file, want delete file read (the input file). trhis using:
public void deleteinputfile(string inputfile) { file filetodelete = new file(inputfile); if(filetodelete.delete()){ system.out.println(filetodelete.getname() + " deleted!"); }else{ system.out.println("delete operation failed."); } } all program works fine, delete opretion fails. response:
delete operation failed.
i think reader close input file after reads it.
the question is, showld delete input file after writing file need?
make sure close input stream , delete file.
you can delete files, directories or links. symbolic links, link deleted , not target of link. directories, directory must empty, or deletion fails.
the files class provides 2 deletion methods.
the delete(path) method deletes file or throws exception if deletion fails. example, if file not exist nosuchfileexception thrown. can catch exception determine why delete failed follows:
try { files.delete(path); } catch (nosuchfileexception x) { system.err.format("%s: no such" + " file or directory%n", path); } catch (directorynotemptyexception x) { system.err.format("%s not empty%n", path); } catch (ioexception x) { // file permission problems caught here. system.err.println(x); } the deleteifexists(path) method deletes file, if file not exist, no exception thrown. failing silently useful when have multiple threads deleting files , don't want throw exception because 1 thread did first.
taken https://docs.oracle.com/javase/tutorial/essential/io/delete.html
here example http://www.mkyong.com/java/how-to-delete-file-in-java/
Comments
Post a Comment