我目前正在构建一个项目,我偶然发现了一个问题,我需要使用java与usb进行通信。 通过沟通,我的意思是像从pc到usb和副vresa的文件传输。 那个porpuse有没有合适的api?
答案 0 :(得分:1)
你可以使用这段代码来使用java来检测usb:
import java.io.*;
import javax.swing.*;
public class DetectWin extends JFrame
{
JPanel loginPnl = new JPanel();
JTextField Dfield = new JTextField("", 20);
DetectWin()
{
super("AUTHORIZATION");
setSize(350, 100);
setLocation(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(loginPnl);
setVisible(true);
setResizable(false);
loginPnl.add(Dfield);
String[] letters = new String[]{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "Z"};
File[] drives = new File[letters.length];
boolean[] isDrive = new boolean[letters.length];
for ( int i = 0; i < letters.length; ++i )
{
drives[i] = new File(letters[i]+":/");
isDrive[i] = drives[i].canRead();
}
Dfield.setText("Waiting for devices....");
while(true)
{
for ( int i = 0; i < letters.length; ++i )
{
boolean pluggedIn = drives[i].canRead();
if ( pluggedIn != isDrive[i] )
{
if ( pluggedIn )
Dfield.setText("Drive "+letters[i]+" has been plugged in");
else
Dfield.setText("Drive "+letters[i]+" has been unplugged");
isDrive[i] = pluggedIn;
}
}
try { Thread.sleep(100); }
catch (InterruptedException e) { }
}
}
public static void main(String args[])
{
DetectWin AUTHORIZATION = new DetectWin();
}
}