zip,move and size of file which is in a server path using java -
i have file in server, want create 3 java apis, below 3 operations in dependently.
- get file size
- move file different file name different server location
- zip file
in existing code executing linux commands perform operations, unfortunately, linux commands not getting executed, due server/set issue, forced use java commands.(we use jdk 1.6)
i not java developer. have gone through of answered questions, not explaining file in server path. appreciated.
to file size in bytes:
file file = new file("filename.txt"); long filesize = file.length();
to move file must first copy , delete original:
inputstream instream = null; outputstream outstream = null; try { file fromfile = new file("startfolder\\filename.txt"); file tofile = new file("endfolder\\filename.txt"); instream = new fileinputstream(fromfile); outstream = new fileoutputstream(tofile); byte[] buffer = new byte[1024]; int length; while ((length = instream.read(buffer)) > 0){ outstream.write(buffer, 0, length); } instream.close(); outstream.close(); fromfile.delete(); } catch(ioexception e) { e.printstacktrace(); }
to zip file:
byte[] buffer = new byte[1024]; try { fileinputstream filetozip = new fileinputstream("filename.txt"); fileoutputstream fileoutputstream = new fileoutputstream("filename.zip"); zipoutputstream zipoutputstream = new zipoutputstream(fileoutputstream); zipentry zipentry= new zipentry("filename.txt"); zipoutputstream.putnextentry(zipentry); int len; while ((len = filetozip.read(buffer)) > 0) { zipoutputstream.write(buffer, 0, len); } filetozip.close(); zipoutputstream.closeentry(); zipoutputstream.close(); } catch(ioexception e) { e.printstacktrace(); }
wiki
Comments
Post a Comment