Trying to create a file to save an image Java -
i following exception when trying create file on windows 7 using java. example of path "c:/g-ecx/images-amazon/com/images/g/01/gno/images/orangeblue/navpackedsprites-us-22.v183711641.png". if hard code in path work however. i've been banging head 2 hours, can help.
mkdir fails doesn't through exception, create file throws exception.
java.io.ioexception: system cannot find path specified @ java.io.winntfilesystem.createfileexclusively(native method) @ java.io.file.createnewfile(file.java:883) @ org.willmanning.mtt.html.processingbehavior.imageprocessingbehavior.processimage(imageprocessingbehavior.java:125) @ org.willmanning.mtt.html.processingbehavior.imageprocessingbehavior.loadimages(imageprocessingbehavior.java:99) @ org.willmanning.mtt.html.processingbehavior.imageprocessingbehavior.processnodes(imageprocessingbehavior.java:66) @ org.willmanning.mtt.html.processingbehavior.imageprocessingbehavior.processrootnode(imageprocessingbehavior.java:34) @ org.willmanning.mtt.html.parsingfacade.processurl(parsingfacade.java:38) @ org.willmanning.mtt.app.main(app.java:45) /** * * @param image * @param url */ public void processimage(bufferedimage image, url url) { stringbuilder path = new stringbuilder(); path.append("c:/users/will/documents/"); path.append(url.gethost().replace('.', '/')); path.append(url.getfile()); path.replace(path.lastindexof("."), path.length(), ".txt"); file file = new file(path.tostring()); boolean mkdir = file.mkdir(); boolean isnew = false; try { isnew = file.createnewfile(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } /* * create file if doesn't exist */ if (isnew) { try { imageio.write(image, "jpg", file); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
try using
boolean mkdir = file.mkdirs(); instead of
boolean mkdir = file.mkdir(); mkdirs() creates whole parent path/directories if needed:
Comments
Post a Comment