Java cd命令只向前抛出文件夹,而不返回输入?
关于构建某种文件管理器(仅限于控制台应用程序)的初学者考试,我被CD命令卡住了,这是我的代码行,有什么想法吗?谢谢Java cd命令只向前抛出文件夹,而不返回输入?,java,Java,关于构建某种文件管理器(仅限于控制台应用程序)的初学者考试,我被CD命令卡住了,这是我的代码行,有什么想法吗?谢谢 public void cd(String arr[]) { File file = new File(currentFolder.getPath() + "\\" + arr[1]); if (file.exists() && file.isDirectory()) currentFolder = file; } 我在Mai
public void cd(String arr[]) {
File file = new File(currentFolder.getPath() + "\\" + arr[1]);
if (file.exists() && file.isDirectory())
currentFolder = file;
}
我在Main
类中调用此方法,仅适用于前面的示例,首先命令create Fruit
,然后进入C:\\Fruit
,然后我在文件夹Fruit
中创建文件夹apple
,然后使用CD
命令C:\\Fruit\apple
,进入apple文件夹,但是如果我再把CD水果放到C:\\Fruit上,我就回不去了?
我从主方法btw中的开关调用这个CD
和Create
命令并运行
来自主代码的部分:
boolean run = true;
Scanner scan = new Scanner(System.in);
Application app = new Application(); // Application class where is cd method from above
while (run) {
System.out.println("Select the 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()) { ....
您可以使用File.getParent()
或File.getParentFile()
向上移动层次结构
假设您的currentFile
变量指向c:/xxx
使用cd yyy
时,当前文件应指向c:/xxx/yyy
使用cd..
可以执行如下操作:currentFile=currentFile.getParentFile()
如果您在C:\Fruit and cd to apple中,则当前文件夹为C:\Fruit\apple。如果您对水果执行另一个cd命令,它将查找C:\Fruit\apple\Fruit
在命令提示下,要进入文件夹,请使用..,因此应用程序的命令应该是,而不是cd-Fruit
cd ..
如果你想上下一个,你也可以从树上下来
cd ../banana
或者在窗户上
cd ..\banana
getPath和getCanonicalPath识别。。字符,因此如果您将正确的参数传递给应用程序,代码将正常工作。您的问题是什么?考虑更新你的问题并提供更多关于问题的细节。读评论/我把这个方法叫做…