Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:12+00:00 2026-06-17T16:46:12+00:00

What I need to do is take a path to search for a file

  • 0

What I need to do is take a path to search for a file from the user and also take a set of characters from the user which is a part of the filename to be searched. Once the file is found then it would display all the locations including sub-folders along with complete filename.What I have done is taken all the files in the given path and put them in a File[] array using ListFiles().Getting the names of all the files and save them into an arraylist. Make another arraylist and save only the filename without the extension from the previous arraylist. Then use the set of characters entered by the user to compare with each filename in this arraylist.To test if this much was working I printed it out but its printing all the file names and not just the ones that match the character set the user entered.Here is my code:

import java.util.*;
import java.io.*;

public class FileSearch
 {
  public static void main(String args[])
   {
    try
     {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("  1.Enter the path to search:");
        String str = br.readLine();
        System.out.println("  2.Enter some characters of the filename:");
        String str1 = br.readLine();

        File files = new File(str);

        File[] f = files.listFiles();     

        List<String> li = new ArrayList<String>();
        List<String> li1 = new ArrayList<String>();

        for(int i=0;i<f.length;i++)
        {
            if(f[i].isFile())
            {
                li.add(f[i].getName());
            }
        }

        for(int i=0;i<li.size();i++)
        {
           int j = li.get(i).lastIndexOf('.');
           li1.add((j>-1)?li.get(i).substring(0, j):li.get(i));
        }

        for(int i=0;i<li1.size();i++)
         {
            if(li1.get(i).contains(str1));
            {
                System.out.println(li1.get(i));
            }
        }
     }
    catch(Exception e)
     {
        e.printStackTrace();
     }
  }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:46:13+00:00Added an answer on June 17, 2026 at 4:46 pm

    Your code is absolutely fine. The following statements is the root cause of the problem.

    if(li1.get(i).contains(str1));
    {
        System.out.println(li1.get(i));
    }
    

    If you have observed clearly you have used ; after the if condition. Remove the semi colon the code would work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string: string path = C:\Users\Username\Desktop\FileName.pdf; I need to take off
I have a html file which select images from user's computer.code is given bellow
I need to take a large file, with lines such as: member: cn=user0001,ou=people And
i need to take some row. They came from sql TARIH (sql column) is
I need to take rows from two separate tables, and arrange them in descending
I need to take a url, /ServiceSearch/r.php?n=blahblah, and have it go to /search/blahblah/ so
I need to take two strings and combine them into a single path string
In my application I need to search for a network path and do some
I need to write a program that will take a existing local windows user,
I'm trying to find the right direction and path i need to take in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.