【问题标题】:Array of files and folders in current directory java and valid string当前目录 java 中的文件和文件夹数组和有效字符串
【发布时间】:2016-01-23 16:34:46
【问题描述】:

我得到一个包含 jar 文件当前目录中所有文件和文件夹的数组。

下一步,从用户那里获取字符串输入(文件或文件夹的名称...), 并在数组中搜索该字符串。 array.indexOf 之类的东西,如果存在,打印文件的完整路径。

String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);

System.out.println("Give Me The Name Of The File You Search In current Directory");
String specific = in.nextLine();

File f = new File(workingDir);
ArrayList<File> files = new ArrayList<File>(Arrays.asList(f.listFiles()));

System.out.println(files);

if (files.contains(specific)) {
System.out.println("File exsist");
}

下一步该怎么做?

** 解决了。谢谢

【问题讨论】:

  • 那么你不应该使用 files.contains(specific) 吗?不完全确定你的问题是什么。如何在字符串数组中搜索字符串,或者如何打印完整的文件路径?
  • 两者。首先搜索一个字符串,然后打印包含该字符串在数组中的完整文件路径。
  • 您应该通过单击答案旁边的大复选标记来接受对您有帮助的答案。

标签: java arrays string scanning


【解决方案1】:

如果您想打印名称存储在“特定”字符串中的文件的完整文件路径,那么您应该这样做。

String workingDir = System.getProperty("user.dir");
    System.out.println("Current working directory : " + workingDir);

    System.out.println("Give Me The Name Of The File You Search In current Directory");
    String specific = in.nextLine();

    File f = new File(workingDir);
    ArrayList<File> files = new ArrayList<File>(Arrays.asList(f.listFiles()));

    System.out.println(files);

    for (File file : files) {
        if (file.getName().equals(specific)) {
            System.out.println(file.getAbsolutePath());
        }
    } 

【讨论】:

    【解决方案2】:

    你要找的是

    if(files.get(x).getName().contains(fileString))
    

    或者完全回答:

      for(File file : files){
      if(file.getName().contains(specific)){
          // process file
          System.out.println("Full filepath is " + workingDir + "/" + specific);
      }
    }
    

    编辑:

    还要检查它是否从未被发现:

      boolean wasFound = false;
      for(File file : files){
      if(file.getName().contains(specific)){
          // process file
          System.out.println("Full filepath is " + workingDir + "/" + specific);
          wasFound = true;
      }
    }
    
       if (!wasFound) {
         // File was not found, show an error
       }
    

    【讨论】:

    • 嘿,谢谢。 var x 是什么意思?只是为了理解
    • 非常感谢,它得到了几乎所有的东西。但是,在 else 语句中,我尝试打印错误消息,并打印数组中 evrey 项目的消息。我如何才能返回 false 直到它为 true,或者扫描结束而不匹配字符串,然后推送错误消息?
    • 在开始时设置一个布尔值,如果它的值永远不会改变,则意味着您从未找到该文件。我会为你编辑。
    【解决方案3】:

    所以你的代码应该是这样的,如果字符串比较名称:

     for (int i = 0; i < files.size(); i++){
       File file = files.get(i);
       if (file.getName().contains("yourFileName")){
          System.out.println("File Exists");
        } 
     }
    

    【讨论】:

    • 编辑我的问题。当我尝试这个时,它不会提示“文件存在”,即使它是真的。
    • @EyalCohen 好的。我的错。试试这个解决方案。
    【解决方案4】:

    首先搜索一个字符串

    您可以使用完整路径创建带有输入的文件对象,因为ArrayList files 包含文件的完整路径。比如:

    File toCompare = new File(workingDir+System.getProperty("file.separator")+specific);
    

    然后看看它是否存在于列表中:

    if(files.contains(toCompare))
    

    然后打印包含数组中字符串的完整文件路径 然后打印完整路径:

        System.out.println(toCompare);
    

    否则说找不到:

    else
        System.out.println("not found");
    

    把它们放在一起:

    File toCompare = new File(workingDir+System.getProperty("file.separator")+specific);
    if(files.contains(toCompare))
        System.out.println(toCompare);
    else
        System.out.println("not found");
    

    【讨论】:

    • 非常感谢好人。非常符合我的要求。
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多