Java cd command going only foward throw folders, not back on input? -
beginner exam on building kind of file manager (console app only), i'm stuck on cd command, line of code, ideas? thanks
public void cd(string arr[]) { file file = new file(currentfolder.getpath() + "\\" + arr[1]); if (file.exists() && file.isdirectory()) currentfolder = file; } i'm calling method in main class , works foward example, first command create fruit, there goes c:\\fruit, l create folder apple in folder fruit , l go apple folder cd command c:\\fruit\apple, l cant go if l put cd fruit c:\\fruit again? i'm calling cd , create commands switch in main method btw , works
part main code:
boolean run = true; scanner scan = new scanner(system.in); application app = new application(); // application class cd method above while (run) { system.out.println("select command:"); system.out.println("create, list, info, copy, move, rename, cd, delete, exit"); system.out.println("path: " + app.getcurrentfolder().getpath()); string cmd = scan.nextline(); string arr[] = cmd.split(" "); switch (arr[0].touppercase()) { ....
you can use file.getparent() or file.getparentfile() move hierarchy.
assume currentfile variable points c:/xxx.
with cd yyy currentfile should point c:/xxx/yyy
with cd .. this: currentfile = currentfile.getparentfile().
Comments
Post a Comment