你好我正在使用if (canRead && canWrite && !isFloppy && isDrive)
,它只會讀取它找到的第一個驅動器「C:\」,我有一個硬盤和一個SSD,由於某種原因anyehelp不會檢測到某個原因「D:\」的ssd?謝謝。驅動器檢測只檢測一個驅動器?
對不起球員我忘了給包括增值經銷商:
package javaapplication3;
import java.io.*;
import javax.swing.filechooser.FileSystemView;
class filler
{
public static void main(String ar[]) throws InterruptedException
{
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();
//(type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile"))
if (canRead && canWrite && !isFloppy && isDrive) {
try {
File file = new File(drive +"log_22_2112321321312.log");
if (file.createNewFile()){
System.out.println("File is created!");
}
if (file.exists()){
System.out.println("Drive found " + drive);
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (canRead && canWrite && !isFloppy && isDrive &&(type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile"))) {
try {
File file = new File("log_22_2112321321312.log");
if (file.createNewFile()){
System.out.println("File is created!");
}
if (file.exists()){
System.out.println("Drive found " + drive);
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
你如何確定'canRead','canWrite','isFloppy'和'isDrive'? – MadProgrammer
這些是他必須創建的變量。可能的來源:http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java – collinjsimpson
好找到xD精確來源加上他們在後的 – Fusion