Search file program in Java

light2

Recruit
I'd want to create a file search tool in Java that works on both Linux and Windows. I'm familiar with Windows, but I'm not familiar with Linux. This logic is being used to display all of the discs in the windows.

Java:
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]);

    }
}
}

When used in Windows, the above code displays all of the roots such as c:, d:, and so on, but when used in Linux, it just displays /. I'm also using this reasoning to find a certain file in Windows.

Java:
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.
 
Linux has only one root, which is always /

It is showing the correct value. Just as you would traverse all directories recursively under all drives in windows, traverse all directories recursively in / for Linux.
 
Linux has only one root, which is always /

It is showing the correct value. Just as you would traverse all directories recursively under all drives in windows, traverse all directories recursively in / for Linux.
yes right
You are correct that Linux has only one root directory, which is always "/", and the code will need to traverse all directories recursively under the root directory to find the desired file.
btw got a solution:
To design a Java file search tool that works on both Linux and Windows, you must consider the differences between the two operating systems. The way file paths are expressed is one significant distinction.
Backslashes () are used in Windows to divide folders, whereas forward slashes (/) are used in Linux. Furthermore, Windows has the idea of drive letters, which Linux does not have.
To make the code operate on both operating systems, you must adopt a cross-platform method to file path management. The File.separator constant in Java represents the file path separator for the current platform.
 
Back
Top