通过用户输入的文件名搜索特定文件夹中的文件是否存在?

问题描述 投票:0回答:1

我正在尝试构建一个程序,该程序允许用户输入任何名称,它将在特定的已定义文件夹中搜索。如果文件夹中存在命名文件,则表明文件存在,否则不存在。

import java.io.*;
import java.util.*;
class FindFile 
{
    public void findFile(String name,File file)
    {
        File[] list = file.listFiles();
        if(list!=null)
        for (File fil : list)
        {
            if (fil.isDirectory())
            {
                findFile(name,fil);
            }
            else if (name.equalsIgnoreCase(fil.getName()))
            {
                System.out.println(fil.getParentFile());
            }
        }
    }
    public static void main(String[] args) 
    {
        FindFile ff = new FindFile();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the file to be searched.. " );
        String name = scan.next();
        System.out.println("Enter the directory where to search ");
        String directory = scan.next();
        ff.findFile(name,new File(directory));
    }
}
java netbeans
1个回答
0
投票

您的问题不清楚。使用您的代码,您将在特定文件夹中以及从子文件夹中获得所有文件


public static void main(String[] args) {
    FindFile ff = new FindFile();
    String name = "notepad.exe";
    String directory ="C:\\Windows\\";

    ff.findFile(name,new File(directory));

    System.out.println("---------------");
}

输出:

C:\ Windows \ notepad.exe C:\ Windows \ System32 \ notepad.exe C:\ Windows \ SysWOW64 \ notepad.exe C:\ Windows \ winsxs \ amd64_microsoft-windows-notepadwin_31bf3856ad364e35_6.1.7600.16385_none_9ebebe8614be1470 \ notepad.exe .-----------------------------------------------

如果要在one文件夹中搜索文件(不更深)并且您使用的是[[NOT区分大小写的[[OS

OP: 用户输入文件名,然后在特定文件夹中搜索(如果存在)或 不是..
public static void main(String[] args) { String filename = "notepad.exe"; String path ="C:\\Windows\\"; File f = new File(path+"\\"+filename); if(f.exists() && f.isFile()) { System.out.println(path+file+" : found"); } System.out.println("---------------"); }

输出:

  

C:\ Windows \ notepad.exe:找到 .---------------

如果要在一个文件夹中搜索区分大小写的文件名将findFile()代码更改为

public void findFile(String name,File file) { File[] list = file.listFiles(); if(list!=null) { for (File fil : list) { if (name.equalsIgnoreCase(fil.getName())) { System.out.println(fil.getParentFile()+"\\"+name); } } } }


UPDATE

将其放在您的主要内容中,然后将搜索字词替换为所需的内容。

public static void main(String[] args) { String filename = "notepad.exe"; String path ="C:\\Windows"; File f = new File(path+"\\"+filename); if(f.exists() && f.isFile()) { System.out.println(path+filename+" : found"); } else { System.out.println(path+filename+" : not found"); } }

UPDATE 2

查看您的代码具有三个新组件

jButton3jsearchTextjLabel3

您可以使用它并将其扩展到您自己的“表单对话框”。

enter image description here

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { searchtext = jsearchText.getText(); File f = new File(searchtext); if(f.exists() && f.isFile()) { jLabel3.setText("found : "+searchtext); } else { jLabel3.setText("not found : "+searchtext); } }

UPDATE 3

您的问题:带有多扩展名的支票

public static void main(String[] args) { String fullPath; String pathSeparator = "\\"; String extSeparator = "."; String path = "U:\\Grafik\\Stack\\testExt\\"; String filename = "Pic24.gif"; String searchtext; String[] elements = { ".jpg",".png",".jpeg"}; fullPath = path+filename; int dot = fullPath.lastIndexOf(extSeparator); searchtext = fullPath.substring(0, dot); for (String s: elements) { File f = new File(searchtext+s); if(f.exists() && f.isFile()) { System.out.println("found : "+searchtext+s); } else { System.out.println("not found : "+searchtext+s); } } } }
© www.soinside.com 2019 - 2024. All rights reserved.