java - ProcessBuilder delete and rename -
i having processbuilder
should delete file.txt
, rename newfile.txt
. problem both files deleted. idea why , how fix?
public class myprocessbuilder { public static void main(string[] args){ final arraylist<string> command = new arraylist<string>(); // create files file file = new file("file.txt"); file newfile = new file("newfile.txt"); try{ if(!file.exists()) file.createnewfile(); if(!newfile.exists()) newfile.createnewfile(); } catch(exception e){} // force remove file.txt command.add("rm"); command.add("-f"); command.add("file.txt"); // rename newfile.txt file.txt command.add("mv"); command.add("newfile.txt"); command.add("file.txt"); final processbuilder builder = new processbuilder(command); try { builder.start(); } catch (ioexception e) { e.printstacktrace(); } } }
the issue running single command, namely
rm -f file.txt mv newfile.txt file.txt
this unconditionally deletes files named file.txt
, mv
, newfile.txt
.
you want split 2 separate commands.
better still, use file.delete()
, file.renameto()
. not give more control, make code more portable.
Comments
Post a Comment