java - Exception when moving files -
i'm assuming has limited knowledge of how filevisitor works , parses directories. i'm trying move contents of directory directory. i'm doing implementing filevisitor<path> this:
public class mover implements filevisitor<path> { private path target; private path source; public mover(path source, path target) { this.target = target; this.source = source; } @override public filevisitresult previsitdirectory(path dir, basicfileattributes attrs) throws ioexception { path targetdir = target.resolve(source.relativize(dir)); try { files.move(dir, targetdir); } catch (filealreadyexistsexception e) { if(!files.isdirectory(targetdir)) { system.out.println("throwing e!"); throw e; } } return filevisitresult.continue; } @override public filevisitresult postvisitdirectory(path file, ioexception exc) throws ioexception { return filevisitresult.continue; } @override public filevisitresult visitfile(path file, basicfileattributes attrs) throws ioexception { try { files.move(file, target.resolve(source.relativize(file))); } catch (nosuchfileexception e) { //todo: figure out why exception raised! system.out.println("nosuchfileexception"); } return filevisitresult.continue; } @override public filevisitresult visitfilefailed(path file, ioexception exc) throws ioexception { return filevisitresult.continue; } } in turn use class mover this:
files.walkfiletree(from, new mover(from, to)); i don't add from twice when calling walkfiletree, problem line under todo in code (however appreciate comments on how solve to). don't understand why exception raised. i'm guessing because file moved. if case how go stop code trying move again, way i'm doing more or less correct?
below function programmatically move file
set correct permissions in manifest
<uses-permission android:name="android.permission.write_external_storage" />
private void movefile(string inputpath, string inputfile, string outputpath) { inputstream in = null; outputstream out = null; try { //create output directory if doesn't exist file dir = new file (outputpath); if (!dir.exists()) { dir.mkdirs(); } in = new fileinputstream(inputpath + inputfile); out = new fileoutputstream(outputpath + inputfile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; // write output file out.flush(); out.close(); out = null; // delete original file new file(inputpath + inputfile).delete(); } catch (filenotfoundexception fnfe1) { log.e("tag", fnfe1.getmessage()); } catch (exception e) { log.e("tag", e.getmessage()); } } to delete file use
private void deletefile(string inputpath, string inputfile) { try { // delete original file new file(inputpath + inputfile).delete(); } catch (filenotfoundexception fnfe1) { log.e("tag", fnfe1.getmessage()); } catch (exception e) { log.e("tag", e.getmessage()); } }
to copy
private void copyfile(string inputpath, string inputfile, string outputpath) { inputstream in = null; outputstream out = null; try { //create output directory if doesn't exist file dir = new file (outputpath); if (!dir.exists()) { dir.mkdirs(); } in = new fileinputstream(inputpath + inputfile); out = new fileoutputstream(outputpath + inputfile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; // write output file (you have copied file) out.flush(); out.close(); out = null; } catch (filenotfoundexception fnfe1) { log.e("tag", fnfe1.getmessage()); } catch (exception e) { log.e("tag", e.getmessage()); } }
Comments
Post a Comment