io - Two similar copy codes with Java, two behaviors -
so wanted implement function copies file new file of i'll specify directory (i'll create it) , then, found on stackoverflow, use files.copy function of apache.commons trick.
my problem following : write 2 codes, 1 works , other don't, except similar seem not capture difference...
here's code first method :
public static void copytofile2 (string firmfoldername, string allfirmsfoldername, string copy_file_name, string copied_file_name) throws ioexception { file = new file(copied_file_name) ; string pathofdirectoryoftofile= "folder/" + allfirmsfoldername +"/" + firmfoldername ; //string pathofdirectoryoftofile = "folder/fomrs/firm/" ; string pathoftofile = pathofdirectoryoftofile + "/" + copy_file_name ; (new file(pathofdirectoryoftofile)).mkdir(); file = new file(pathoftofile) ; files.copy( from.topath(), to.topath() ); }
in one, have specify few parameters forge path directory, create directory , create copy file in there. throws nosuchfileexception, , while know file doesn't not exist, thought might nice , create itself, since didn't : went ahead , added to.createnewfile();
right after file instanciation make sure, i'll have following code :
(new file(pathofdirectoryoftofile)).mkdir(); file = new file(pathoftofile) ; to.createnewfile(); files.copy( from.topath(), to.topath() );
with one, ioexception stating specified access path not found !
second method :
public static void copytofile1 (string firmfoldername, string allfirmsfoldername, string copy_file_name, string copied_file_name) throws ioexception { file = new file(copied_file_name) ; string pathofdirectoryoftofile= "folder/" + allfirmsfoldername +"/" + firmfoldername +"/" ; string pathoftofile = pathofdirectoryoftofile + "/" + copy_file_name ; (new file("folder/mdjs55/")).mkdir(); file = new file("folder/mdjs55/tm.jsp" ) ; files.copy( from.topath(), to.topath() ); }
in 1 works fine.
so ? difference can not path in copytofile2 dynamic , in second static, how supposed problem ? know it's merely string that's being build...
p.s : used system.out.println(to.topath()) check out path that, it's constructed.
thanks in advance help.
the path in second example shorter. mkdir()
create 1 sub-directory if go 2 sub-directories fail , when try use file in directory fail.
i suspect want mkdirs()
creates multiple-levels of directories required.
Comments
Post a Comment