Issue
I am trying to build a program which allow user to enter any name and it will search in specific defined folder. If named file exist in folder then it shows file exist otherwise not exist.
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));
}
}
Solution
Your Question is not clear.
With your code you get all Files in the specific folder and also from the child folders
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("---------------");
}
Output:
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 .-----------------------------------------------
If you want to search for a file in one folder (not deeper)
and you are on a NOT case sensitive OS
OP :
User enter file name then it Search in Specific folder if Exist Or Not..
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("---------------");
}
Output:
C:\Windows\notepad.exe : found
.---------------
If you want in one folder to search for case sensitive file names
change the findFile() code to
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
Put this in your main and replace the search terms with what you are looking for.
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
Looking at your code With three new components
jButton3
jsearchText
jLabel3
you can use and expand it to your own "Form dialog".
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
To your question : checks with multi extension
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);
}
}
}
}
Answered By - moskito-x