[英]Java codes to run program from detected drive
I have a program that I want to run from a detected USB drive (removable storage such as a USB), and this was done by creating two classes: external.java and DetectDrive.java as follows: 我有一个要从检测到的USB驱动器(可移动存储设备,例如USB)运行的程序,这是通过创建两个类来完成的,如下所示:external.java和DetectDrive.java:
external.java external.java
public class external
{
public static void main(String args[]) throws InterruptedException
{
DetectDrive d = new DetectDrive();
String DetectDrive = d.USBDetect();
BufferedWriter fileOut;
String filePath = DetectDrive;
System.out.println(filePath);
try
{
fileOut = new BufferedWriter(new FileWriter("F:\\external.bat"));
fileOut.write("cd "+ filePath +"\n");
fileOut.write("external.exe"+"\n");
fileOut.close(); //close the output stream after all output is done
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cmd /c start" +DetectDrive+ "\\external.bat");
p.waitFor();
} catch (IOException e){
e.printStackTrace();
}
}
}
DetectDrive.java 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;
}
}
The problem now is that there are no errors when I run the external.java (main). 现在的问题是,当我运行external.java(主要)时没有错误。 However, the output only shows the detected drive which is F:\\ but it doesn't run the specified program which is external.exe and it also mentioned that the program got terminated.
但是,输出仅显示检测到的驱动器F:\\,但未运行指定的程序external.exe,并且还提到该程序已终止。 Can someone please help me point out where I went wrong and what the correct codes should be like?
有人可以帮我指出我出了什么问题以及正确的代码是什么样的吗? I am new to Java.
我是Java新手。
I got the DetectDrive codes from http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java which I believe is now in maintenance. 我从http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java中获得了DetectDrive代码,我认为该代码正在维护中。
Is it possible to change the new FileWriter(" F:\\external.bat ") to detect the USB drive directory instead? 是否可以更改新的FileWriter(“ F:\\ external.bat ”)来检测USB驱动器目录? For example letting the program detect the usb drive and automatically put in the correct directory instead of us typing the F:\\ manually.
例如,让程序检测USB驱动器并自动将其放置在正确的目录中,而不用我们手动输入F:\\。 I have no answer for this yet.
我还没有答案。 Please help!
请帮忙!
It seems problem is with command : 似乎问题出在命令:
"cmd /c start" +DetectDrive+ "\\external.bat"
It should have a {space}
after start: 启动后应该有一个
{space}
:
"cmd /c start " +DetectDrive+ "\\external.bat"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.