Make program search file in Java [message #1858038] |
Sun, 12 March 2023 17:18 |
bretny relly Messages: 14 Registered: March 2023 |
Junior Member |
|
|
I can write a file search software for Windows, but I know nothing about Linux. This logic is being used to display all of the discs 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]);
}
}
}
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.
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 works good, but my difficulty is figuring out how to do it in Linux; I'm new to Linux and have no idea how to do it, and I'm running out of time; any assistance would be greatly appreciated.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.27851 seconds