gpt4 book ai didi

java - 如何检查用户输入的路径是否正确?

转载 作者:行者123 更新时间:2023-12-01 14:48:33 25 4
gpt4 key购买 nike

我需要证明用户输入路径。
因为当用户给出的不是文件夹路径而是文件路径时。节目“倒下”。
我知道这是一个错误,但如何确保用户路径正确。

代码:

 class PathAndWord {
final String path;
final String whatFind;

PathAndWord(String path, String whatFind) {
this.path = path;
this.whatFind = whatFind;
}

boolean isProperlyInitialized() {
return path != null && whatFind != null;
}
}

public void askUserPathAndWord() {
try {
tryToAskUserPathAndWord();
} catch (IOException | RuntimeException e) {
System.out.println("Wrong input!");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("Interrupted.");
e.printStackTrace();
}
}

private void tryToAskUserPathAndWord() throws IOException, InterruptedException {
PathAndWord pathAndWord = readPathAndWord();

if (pathAndWord.isProperlyInitialized()) {
performScan(pathAndWord, "GameOver.tmp");
System.out.println("Thank you!");
} else {
System.out.println("You did not enter anything");
}
}

private PathAndWord readPathAndWord() throws IOException {
System.out.println("Please, enter a Path and Word (which you want to find):");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

String path = readPath(bufferedReader);
String whatFind = readWord(bufferedReader);
return new PathAndWord(path, whatFind);
}

private String readPath(BufferedReader bufferedReader) throws IOException {
System.out.println("Please enter a Path:");
return bufferedReader.readLine();
}

private String readWord(BufferedReader bufferedReader) throws IOException {
System.out.println("Please enter a Word:");
return bufferedReader.readLine();
}

private void performScan(PathAndWord pathAndWord, String endOfWorkFileName) throws InterruptedException {
BlockingQueue<File> queue = new LinkedBlockingQueue<File>();

File endOfWorkFile = new File(endOfWorkFileName);
CountDownLatch latch = new CountDownLatch(2);

FolderScan folderScan = new FolderScan(pathAndWord.path, queue, latch,
endOfWorkFile);
FileScan fileScan = new FileScan(pathAndWord.whatFind, queue, latch,
endOfWorkFile);

Executor executor = Executors.newCachedThreadPool();
executor.execute(folderScan);
executor.execute(fileScan);

latch.await();
}

问题:

  • 如何检查用户输入的路径是否正确?
  • 如果路径不正确显示路径错误的消息!再试一次
  • 也能够检查单词 - whatFind 是否正确。
  • 需要在点之前做什么吗?

最佳答案

private String readPath(BufferedReader bufferedReader) throws IOException {
boolean ok = false;
do {
System.out.println("Please enter a Path:");
File f = new File(bufferedReader.readLine());
if(f.exists() && f.isDirectory())
ok = true;
else
System.err.println("Doesn't exist or is not a folder.");
} while(!ok);
return f.getAbsolutePath();
}

编辑:此方法执行任务“从用户读取路径,该路径存在并且是一个目录”。如果用户输入无效路径(不存在或文件),该方法会识别出这一点,警告用户并再次询问他们......一次又一次 - 直到他们回答正确。

如果可以的话,在本地检查数据是一个很好的习惯。稍后调用该方法时,您可以确定它会返回您所期望的内容。

关于java - 如何检查用户输入的路径是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15132146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com