0

I am using Java codes to find a file that ends with a certain extension in a detected removable storage. I am trying to link the two codes together but I am not sure on how I can do so. These are the codes I am using:

DetectDrive.java

import java.io.*;
import java.util.*;
import javax.swing.filechooser.FileSystemView;

public class DetectDrive
{
    public String USBDetect()
    {
        String driveLetter = "";
        FileSystemView fsv = FileSystemView.getFileSystemView();

        File[] f = File.listRoots();
        for (int i = 0; i < f.length; i++)
        {
            String drive = f[i].getPath();
            String displayName = fsv.getSystemDisplayName(f[i]);
            String type = fsv.getSystemTypeDescription(f[i]);
            boolean isDrive = fsv.isDrive(f[i]);
            boolean isFloppy = fsv.isFloppyDrive(f[i]);
            boolean canRead = f[i].canRead();
            boolean canWrite = f[i].canWrite();

            if (canRead && canWrite && !isFloppy && isDrive && (type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile")))
            {
                //log.info("Detected PEN Drive: " + drive + " - "+ displayName); 
                driveLetter = drive;
                break;
            }
        }

        /*if (driveLetter.equals(""))
        {
            System.out.println("Not found!");
        } 
        else 
        {
            System.out.println(driveLetter);
        }
        */

        //System.out.println(driveLetter);
        return driveLetter;
    }
}

FileSarch.java

import java.io.*;

public class FileSearch
{   
    public String find(File dir) 
    {
        String pattern = ".raw";

        File listFile[] = dir.listFiles();
        if (listFile != null) 
        {
            for (int i=0; i<listFile.length; i++) 
            {
                if (listFile[i].isDirectory()) 
                {
                    find(listFile[i]);
                } else 
                { 
                    if (listFile[i].getName().endsWith(pattern)) 
                    {
                        System.out.println(listFile[i].getPath());
                    }
                }
            }
        }
        return pattern;
    }
}

The file that I want the program to search ends with a .raw extension and I want the program to search for the file in the detected removable storage (e.g. F:). How do I link these 2 codes together? If possible I would like an example of codes to link them. I got the codes for FileSearch.java from http://rosettacode.org/wiki/Walk_a_directory/Recursively#Java

user3847620
  • 59
  • 4
  • 9

2 Answers2

0

First, change USBDetect by replacing String driveLetter with File removableDrive, and return that. Then pass the value returned from USBDetect into find.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • When I change it to File, the public String USBDetect() will have to change to public File USBDetect() right? – user3847620 Jul 19 '14 at 05:42
  • Yep. You'll also need to remove the `drive` variable and change `driveLetter = drive;` to `removableDrive = f[i];` – Paul Hicks Jul 19 '14 at 06:10
0

Heres how I would do it, however I would also make the methods USBDetect and find static, they both dont seem to have any objects their referencing in their parent class. Also make USBDetect return a File instead of a String

public static void main(String [] args) {
    // look for the drive
    String drive = (new DetectDrive()).USBDetect();
    // if it found a drive (null or empty string says no)
    if(drive != null && !drive.isEmpty()) {
        // look for a file in that drive
        FileSearch fileSearch = new FileSearch();
        fileSearch.find(new File(drive+":"));
    }
}
ug_
  • 11,267
  • 2
  • 35
  • 52
  • What happens if the removable drive's letter is not fixed? For example , it could be F in one computer and G in another computer. Would it be String drive = " "? – user3847620 Jul 19 '14 at 06:04
  • @user3847620 Shoot sorry just fixed it. That `"F"` shouldnt be there – ug_ Jul 19 '14 at 06:18
  • I've tried your solution and it works perfectly! Thanks! – user3847620 Jul 19 '14 at 06:20
  • It worked perfectly yesterday showing the drive and the file, but when I tried running it again today, the output showed that the program was terminated without any errors. I did not change anything to the codes. Does that mean there's something wrong with the program? – user3847620 Jul 20 '14 at 16:47
  • Possibly, you should debug your program. A good way to start debuging is put some `System.out.println()` statements around your program to help you see why it isnt working anymore. Some good starting places would be: Is the drive not being found?, is the File not being found? – ug_ Jul 20 '14 at 22:27
  • Okay I've place some System.out.println() and from what I noticed, the program could find the drive but the file could not be found. I remembered when the codes were working, I did not change the return type to File. But when I tried it, the output is still the same. – user3847620 Jul 21 '14 at 13:39
  • @user3847620 first... does the file your looking for still exist? If your certain it does then put a `System.out.println(listFile[i])` inside the loop. Debug your program... You did one step now do another, you've successfully found out that the drive is being found the next step is to find out if the file is being looked at. – ug_ Jul 21 '14 at 13:50
  • Yup the file is inside the thumbdrive which is connected. When I try to define the main method to test the code you mentioned, I kept getting 'void is an invalid type for the variable main'. Sorry I am new to Java so I am not really familiar with how to work around the codes. Anyways, thank you so much for your help! Really appreciate it! – user3847620 Jul 21 '14 at 14:25