我创建一个程序,可以创建一个目录,然后将文件放在所述目录中。正如您所看到的,在person指定目录之后,程序应该检查目录是否存在或者是否存在并创建目录(如果它不存在)或者说已经存在目录用这个名字。每当我运行程序时,我放置一个已存在的目录,而不是说该目录已经存在,它会直接跳转到失败消息并继续在现有目录中创建新文件。如何修复代码,以便它提供正确的"文件存在"消息然后继续代码,而不只是说失败消息?
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("123");
此外,如果用户指定的目录是不存在的目录。这发生了......
//Outputs 321 (works for any text value)
如何重新编码它以便它提供正确的错误消息并再次询问用户有效的目录?
代码:
Please enter where you want your file...
/Users/FyuZheN/Desktop/test
[Console] Directory does not exist! Creating one...
[Console] Fail! Couldn't create directory!
[Console] What would you like to call your new file?
test.jar
[Console] Created new file test.jar
Successfully created new file: /Users/FyuZheN/Desktop/test/test.jar
答案 0 :(得分:0)
要再次询问您的代码,请执行以下操作:
while(true){
//ask for new foldername
if(usrdir.equals("exit")){
break;//the user should be able to.leave your program
}
//rest of your code
}
要创建新文件,您需要一个文件夹,所以:
boolean folderexists=false;
//if folder creation worked or if folder exists set to true
if(folderexists){
//ask for filename etc
}