2013-02-28 91 views
0

我需要证明用户输入路径。
因为用户不给文件夹路径而是文件路径。计划“倒下”。
我知道这是一个错误,但如何确保用户路径正确。如何检查用户输入路径是否正确?

代码:

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(); 
} 

Qustions:

  • 如何检查,如果用户输入path是正确的?
  • 如果路径不正确 要显示消息path is wrong! Try again
  • 能够检查词 - whatFind是否正确。
  • 以前需要做点什么吗?
+0

您可以创建一个'新的文件(“givenPath”)'。捕获异常,以便知道路径不正确。如果有效,请检查它是否是目录。 – araknoid 2013-02-28 09:45:08

+1

是不是'new File(path).exists()'和/或'newFile(path).isDirectory()'够了吗? – moeTi 2013-02-28 09:45:15

+0

@araknoid你能更好地展示这个变体吗? – 2013-02-28 20:45:43

回答

1
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(); 
} 

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

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

+0

尽管此代码可能会解决所问的问题,但如果您添加一些描述级别以与您的代码一起解释其解决问题的方式(或本例中的问题),则更好。 – Ren 2013-02-28 10:09:08

+0

@Ren你的意思是哪部分代码? – 2013-02-28 20:47:56

+0

@nazar_art我指的是Petr的代码作为答案发布。如果作者添加一行代码来说明它是如何解决问题的,或者说明它的作用等,那么仅有代码的答案通常会更有帮助。 – Ren 2013-02-28 21:03:13

相关问题