哪位兄弟用过libusb-win32读写U盘,要怎么读写U盘上指定的文件呢?
下面代码是libusb的源代码中的一个测试类,只能进行数组操作?(而且在进行读操作时,老是报错.)怎么指定U盘的文件名呢?
有没有其他的方法操作U盘的?
/* 
 * Java libusb wrapper
 * Copyright (c) 2005-2006 Andreas Schl�pfer <spandi at users.sourceforge.net>
 *
 * http://libusbjava.sourceforge.net
 * This library is covered by the LGPL, read LGPL.txt for details.
 */
package ch.ntb.usb.demo;import ch.ntb.usb.Device;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;/**
 * Demo class to demonstrate simple read and write operations to an USB device.<br>
 * 
 */
public class ReadWrite { private static void logData(byte[] data) {
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]);
// System.out.print("0x" + Integer.toHexString(data[i] & 0xff) +
// " ");
}
System.out.println();
} public static void main(String[] args) {
// get a device instance with vendor id and product id
// Device dev = USB.getDevice((short) 0x8235, (short) 0x0222);
Device dev = USB.getDevice((short) 0xed1, (short) 0x6981);
try {
// data to write to the device
byte[] data = new byte[] { 0, 100, 29, 1 };
// data read from the device
byte[] readData = new byte[data.length]; // open the device with configuration 1, interface 0 and without
// altinterface
// this will initialise Libusb for you
dev.open(1, 0, -1);
// write some data to the device
// 0x03 is the endpoint address of the OUT endpoint 3 (from PC to
// device)
// dev.writeInterrupt(0x03, data, data.length, 2000, false);
dev.writeInterrupt(0x1, data, data.length, 3000, false);
// read some data from the device
// 0x84 is the endpoint address of the IN endpoint 4 (from PC to
// device)
// bit 7 (0x80) is set in case of an IN endpoint
dev.readInterrupt(0x82, readData, readData.length, 2000, true);//这行老是报错... // dev.readInterrupt(0x84, readData, readData.length, 2000, true);
// log the data from the device
logData(readData);
} catch (USBException e) {
// if an exception occures during connect or read/write an exception
// is thrown
e.printStackTrace();
} finally {
try {
dev.close();
} catch (USBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

解决方案 »

  1.   


    这个问题很好的....之前我也想做类似的东西来着...但是java来判断U盘是否插入不太好做....我当时想到的是NIO....因为java操作底层硬件的东西不好..大家都知道的...这个做完了其他应该问题不打..在程序中不会管你是否是U盘....都是一个盘符而已....并不会因为是U盘有特别的操作....我支持你....可以交流一下..