0

Opening USB flash in Java as a RandomAccessFile has problems with seek, length and setLength methods. Small example of code:

    try {
        RandomAccessFile raf = new RandomAccessFile("\\\\.\\G:", "r");
        raf.seek(10);
        raf.setLength((long) 0);
        System.out.println(raf.length());
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

Or there is another problem. This code throws FileNotFoundException, but i can open this path in Windows and see files:

    try {
        File file = new File("G:\\");
        RandomAccessFile raf = new RandomAccessFile(file, "r");
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

What can i do to solve this problem? It's necessary to open flash device as RandomAccessFile to work with it using Fat32-lib

Alex
  • 91
  • 1
  • 8

2 Answers2

0

G:\ is a directory !

You are supposed to use file.list() or file.listFiles() in order to get all the entries of this directory.

Use file.isDirectory() when you want to know wether file is a directory...

Vouze
  • 1,678
  • 17
  • 10
  • I see ; you are trying to use this : http://stackoverflow.com/questions/6106874/use-the-windows-raw-access-apis-to-directly-access-the-usb-drive – Vouze Feb 11 '14 at 10:50
0

From this http://support.microsoft.com/kb/100027/en-us

"When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries."

Of course you cannot use "setLength" as you cannot change the physical length of a flash device with software !

Anyway, with your example, you open the device with read-only flag : you cannot change the size.

Vouze
  • 1,678
  • 17
  • 10
  • I understand your idea. But i call raf.length() in the 1st example causes exception. And in fat32-library there are such calls. I need to do smth with it – Alex Feb 11 '14 at 17:18