Java 如何检查用户输入路径是否正确?

Java 如何检查用户输入路径是否正确?,java,validation,user-input,Java,Validation,User Input,我需要证明用户输入路径。 因为用户提供的不是文件夹路径而是文件路径。程序“下降”。 我知道这是一个bug,但如何确保用户路径正确 代码: class PathAndWord { final String path; final String whatFind; PathAndWord(String path, String whatFind) { this.path = path; this.whatFind = whatFind;

我需要证明用户输入路径。
因为用户提供的不是文件夹路径而是文件路径。程序“下降”。
我知道这是一个bug,但如何确保用户路径正确

代码:

 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;
PathAndWord(字符串路径、字符串whatFind){
this.path=path;
this.whatFind=whatFind;
}
布尔值IsPropertyInitialized(){
返回路径!=null&&whatFind!=null;
}
}
公共无效askUserPathAndWord(){
试一试{
trytoaskuerpathandword();
}捕获(IOException | RuntimeException e){
System.out.println(“输入错误!”);
e、 printStackTrace();
}捕捉(中断异常e){
System.out.println(“中断”);
e、 printStackTrace();
}
}
私有void tryToAskUserPathAndWord()引发IOException、InterruptedException{
PathAndWord PathAndWord=readPathAndWord();
if(pathAndWord.isPropertyInitialized()){
performScan(pathAndWord,“GameOver.tmp”);
System.out.println(“谢谢!”);
}否则{
System.out.println(“您没有输入任何内容”);
}
}
private PathAndWord readPathAndWord()引发IOException{
System.out.println(“请输入路径和单词(您要查找):”;
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(System.in));
字符串路径=读取路径(bufferedReader);
字符串whatFind=readWord(bufferedReader);
返回新路径和单词(路径,whatFind);
}
私有字符串读取路径(BufferedReader BufferedReader)引发IOException{
System.out.println(“请输入路径:”);
返回bufferedReader.readLine();
}
私有字符串读取字(BufferedReader BufferedReader)引发IOException{
System.out.println(“请输入一个单词:”);
返回bufferedReader.readLine();
}
private void performScan(路径和文字路径和文字,字符串endOfWorkFileName)引发中断异常{
BlockingQueue=新建LinkedBlockingQueue();
文件endOfWorkFile=新文件(endOfWorkFileName);
倒计时闩锁=新倒计时闩锁(2);
FolderScan FolderScan=新FolderScan(路径和Word.path、队列、闩锁、,
endOfWorkFile);
FileScan FileScan=新的FileScan(pathAndWord.whatFind、queue、lack、,
endOfWorkFile);
Executor Executor=Executors.newCachedThreadPool();
执行器。执行(folderScan);
executor.execute(fileScan);
satch.wait();
}
问题:

  • 如何检查用户输入的路径是否正确
  • 如果路径不正确 显示
    路径错误的消息!请重试
  • 能够检查word-
    whatFind
    是否正确
  • 需要在点之前做什么
编辑:此方法执行任务“从用户读取路径,该路径存在并且是目录”。如果用户键入无效路径(不存在或文件),该方法将识别此路径,警告用户并再次询问他们。。。一次又一次,直到他们回答正确


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

您可以创建一个
新文件(“givenPath”)
。捕获异常,以便知道路径不正确。如果有效,请检查它是否为dir.is not
new File(path).exists()
和/或
newFile(path).isDirectory()
足够了吗?@aranoid您能否更好地显示此变体?虽然此代码可能会解决问题,但最好在代码中添加一些级别的描述来解释它如何解决问题(或在本例中为问题)。@nazar_art我指的是作为答案发布的Petr代码。如果作者添加一行文字说明如何解决问题,或说明其作用等,则仅代码答案通常更有用。
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();
}