java - Add list of files in a folder -
i developing application , need save files in directory each user. half i've writen, i'm unclear how add files directory.
public boolean addfiles(string name,list<file> files){ string path = "d:\\server repository\\usersfiles\\"; file folder = new file(path + name); if(!folder.exists()) folder.mkdirs(); for(file file:files){ //my code //if ended success return true } return false; }
if don't want 3rd party libs fileutils, , want simple code copy files, can this:
public boolean addfiles(string name, list<file> files) { string path = "d:\\server repository\\usersfiles\\"; file folder = new file(path + name); if (!folder.exists()) { folder.mkdirs(); } try { (file file : files) { fileinputstream fisorigin; fileoutputstream fosdestiny; //channels filechannel fcorigin; filechannel fcdestiny; fisorigin = new fileinputstream(file); fosdestiny = new fileoutputstream(new file(folder.getabsolutepath() + "/" + file.getname())); fcorigin = fisorigin.getchannel(); fcdestiny = fosdestiny.getchannel(); //copy file fcorigin.transferto(0, fcorigin.size(), fcdestiny); fisorigin.close(); fosdestiny.close(); } return true; } catch (exception e) { e.printstacktrace(); return false; } }
Comments
Post a Comment