user2058780
user2058780

Reputation:

How to make file search program in java?

I want to make file search using java that works both for Linux and windows, I am able to make file search program for windows but i am clueless about Linux. I am using this logic to show all the disks in the windows.

package test;

import java.io.File;

public class Test {
public static void main(String[] args) {
    File[] drives = File.listRoots();
    String temp = "";
    for (int i = 0; i < drives.length; i++) {
        temp += drives[i];
    }

    String[] dir = temp.split("\\\\");
    for (int i = 0; i < dir.length; i++) {
        System.out.println(dir[i]);

    }
}
}

Above code when used in windows then it will show all the roots like c:,d: etc adn when it is used in Linux it shows only /. And i am using this logic to search specific file in windows.

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

It is working fine but my problem is how to make it in Linux, i am new to Linux so i am clueless how to make it, I am running out of time, any help will be very much helpful for me.

Upvotes: 2

Views: 1276

Answers (5)

archer7
archer7

Reputation: 1

public boolean retrieveFile(String directorypath, String filename ) { boolean found = false; int state = 100;

try
{                   
    File dir = new File(directorypath); 
                            
    String  temp = dir.getAbsolutePath().concat("\\" + filename )
                            
    File[] subdirectories =  dir.listFiles();
                            
    for( int n=0; n<subdirectories.length; n++ )
    {
        String subdirectorypath = subdirectories[n].getAbsolutePath(); 
                                    
        state = temp.compareTo( subdirectorypath );
                                    
        if ( state == 0 ) { found = true;  System.out.println( "File Exists." +  subdirectories[n].getName() ); }       
    }

}
catch(Exception E) {  E.printStackTrace();  }
                
return found;                   

}

Upvotes: 0

Simmant
Simmant

Reputation: 1513

in the Linux you need to point your code to the "home directory" because in Linux os family root is only the one . For file search you need do point make recursive in that directory for more have a look of this tutorial .

http://www.mkyong.com/java/search-directories-recursively-for-file-in-java/

Upvotes: 0

Salah
Salah

Reputation: 8657

You need to use a forward slash instead of back slashs, split the files using

File.separator

That should work on both Linux and Windows

Upvotes: 1

Peter Keller
Peter Keller

Reputation: 7636

In Linux/Unix system there is only one root directory: /. From the Linux documentation:

Everything starts from the root directory, represented by /, and then expands into sub-directories instead of having so-called 'drives'.

Upvotes: 0

bknopper
bknopper

Reputation: 1243

Java is platform independent, so why do you believe you have to make a different implementation for Linux?

The platform dependencies are all handled for you by Java.

From the API (http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listRoots()):

public static File[] listRoots()

List the available filesystem roots.

A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely "/". The set of available filesystem roots is affected by various system-level operations such as the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.

This method returns an array of File objects that denote the root directories of the available filesystem roots.

And you don't necessarily have to use File.seperator instead of the backslash as discussed in File.separator vs Slash in Paths.

But it might be a good idea to do it anyway...

Upvotes: 1

Related Questions