我确定之前已经问过这个问题,但我找到的答案都不能与我现有的代码很好地配合.我发布这个问题,以防有一种方法可以做到这一点,而不是完全重做我到目前为止.
这个想法是在将文件和目录从一个驱动器复制到另一个驱动器时显示一个非常基本的进度条.我有一个名为BasicCopy的类,用于将图片,文档,视频和音乐文件夹(Windows机器上的标准)的内容复制到第二个驱动器上备份目录中相同名称的文件夹.到目前为止这是班级:
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
public class BasicCopy {
String username = "";
String mainDrive = "";
String backupDrive = "";
String backupDir = "";
String[] directories;
public BasicCopy(String inDrive, String outDrive, String username){
mainDrive = inDrive;
backupDrive = outDrive;
this.username = username;
createBackupDirectory();
copyDirectories();
close();
}
//Create backup directory
public void createBackupDirectory(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy_HMMSS");
String timestamp = sdf.format(date);
backupDir = backupDrive + ":\\" + …Run Code Online (Sandbox Code Playgroud) 我试图找到一种更有效的方法来从远程 URL 读取文件并将其保存到字节数组中。这是我目前拥有的:
private byte[] fetchRemoteFile(String location) throws Exception {
URL url = new URL(location);
InputStream is = null;
byte[] bytes = null;
try {
is = url.openStream ();
bytes = IOUtils.toByteArray(is);
} catch (IOException e) {
//handle errors
}
finally {
if (is != null) is.close();
}
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我目前将 URL 传递给该方法,该方法使用 InputStream 对象读取文件的字节。此方法使用 Apache Commons IOUtils。但是,此方法调用往往需要相对较长的时间才能运行。当一个接一个地检索数百、数千或数十万个文件时,它会变得很慢。有没有办法改进这种方法,使其运行更有效?我已经考虑过多线程,但我想把它作为最后的手段。
我正在使用一个 Java 程序,该程序调用外部批处理文件并传入一组命令。我在批处理文件中有一个循环,如下所示:
set paramCount=0
for %%x in (%*) do (
set /A paramCount+=1
set list[!paramCount!]=%%x
)
Run Code Online (Sandbox Code Playgroud)
参数是一堆目录,存储为字符串,如下所示:
String[] commands = {"cmd.exe",
"/C",
"C:\Users\user\Documents",
"C:\Users\user\Pictures",
"C:\Users\user\Videos"}
Run Code Online (Sandbox Code Playgroud)
如您所见,我的 for 循环应该循环遍历传递给批处理文件 (%*) 的参数列表,并模拟脚本中的数组(因为命令数组中的前两个元素用于启动命令过程,只留下要循环的目录)。该程序运行得很好,直到有一天,我突然开始收到一条错误消息,内容如下:
Environment variable list[ not defined
Run Code Online (Sandbox Code Playgroud)
我根本没有对批处理文件进行任何更改,它似乎无缘无故地停止工作。如果有必要的信息,我将使用流程构建器来运行该流程:
ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();
Run Code Online (Sandbox Code Playgroud)
据说在批处理文件中对数组使用这种语法是可以的,所以我不确定为什么它不接受它。感谢您在此事上提供的任何帮助。我在使用这个程序时遇到了很多障碍,虽然我已经解决了 90% 的障碍,但剩下的 10% 已经开始让我发疯了!谢谢!
编辑: 我重写了循环并添加了一些 echo 命令以使调试更容易。但是,当我运行批处理文件时,由于回显,屏幕上没有打印任何内容,但我仍然收到相同的错误:
@echo off
setlocal enableDelayedExpansion
set paramCount=0
for %%x in (%*) do (
echo !paramCount!
echo %%x
set list[!paramCount!]=%%x
set /A paramCount=paramCount+1
)
Run Code Online (Sandbox Code Playgroud)
我还忘了提到,当我从 Eclipse 运行 …
我一直在浏览SO和谷歌一段时间来回答这个问题,但我似乎无法找到一个真正有用的.我将从头开始:
我创建了一个Java类,其中包含一个在后台运行批处理文件的方法(命令窗口不会出现).该程序运行良好,但对最终用户来说会有点混乱,因为批处理文件需要一段时间才能完成 - 用户不知道程序是否仍在运行.批处理脚本完成执行后,会出现一个消息对话框,说明它已完成,但是在运行方法和对话框出现之间的一段时间内,看起来好像程序什么也没做.
所以这是我的问题:我非常希望显示一个带有文本区域的新框架,该文本区域显示批处理文件的输出.但是,据我所知,如果不创建临时文件,写入文件,从中读取文件等,这很难做到.如果可能的话,我宁愿避免这样做.因此,我已经决定在进程运行时显示不确定的JProgressBar可能会更好,并在进程完成时关闭它.不幸的是,我不认为Swing可以处理这个问题,因为它需要同时运行多个进程.我听说过SwingWorker,但我不确定在这种情况下如何使用它.我有以下SSCCE,它可以工作,但没有实现进度条.
public myClass(){
public static void main(String[] args){
String[] commands = {"cmd.exe", "/C", "C:\\users\\....\\myBat.bat"};
Process p = Runtime.getRuntime().exec(commands);
p.waitFor()
JOptionPane.showMessageDialog(null, "Process finished!");
}
}
Run Code Online (Sandbox Code Playgroud)
当p.waitFor()等待进程时,屏幕上没有任何内容.我只是希望向用户显示进程仍在运行的内容.思考?谢谢!
我正在尝试找到一种从命令行顺序运行多个Ant构建的方法.我没有能力直接编辑构建文件.这是我有的:
@echo off
cd c:\my\first\buildfile\dir
ant -buildfile build1.xml target1 target2
cd c:\my\second\buildfile\dir
ant -buildfile build2.xml target1 target2 target3
Run Code Online (Sandbox Code Playgroud)
我希望能够为多个项目使用类似的东西.但是,它目前只运行第一个构建(它似乎构建了两个目标)但随后停止,并且不会继续下一个构建.我确信解决方案可能很明显,但这是我第一次从命令行使用Ant.我通常从Eclipse内部运行它.如果可能的话,如何在一个批处理文件中运行多个ant构建?
我正在编写Java程序调用的Windows批处理文件。表示文件目录的许多不同字符串作为参数传递到批处理文件中。我试图弄清楚如何在最后一个“ \”实例之后提取字符串。例如,我有三个目录:
C:\Users\owner\Documents
C:\Users\owner\Videos
C:\Users\owner\Pictures\Trip
Run Code Online (Sandbox Code Playgroud)
我想将其拆分成字符串:
Documents
Videos
Trip
Run Code Online (Sandbox Code Playgroud)
你们如何建议我这样做?
编辑:在此处已询问一个后续问题:Windows批处理文件中的循环:错误:“该命令的语法不正确”
首先,我要说的是,我已经在SO上发现了很多与此问题几乎相同的问题,但是没有一个(我能找到的)解决了这个问题.这些是我看过的一些:
ServiceConnection.onServiceConnected()在绑定到已启动的服务 之后永远不会调用onServiceConnected,在bindService方法之后从未调用过 ServiceConnection :: onServiceConnected,即使Context :: bindService返回true也未调用? OnServiceConnected未被调用
这是我的Android应用程序代码的一部分:
//Local service interface
private INetworkQueryService myNetworkQueryService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Intent intent = new Intent(this, NetworkQueryService.class);
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
}
//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as …Run Code Online (Sandbox Code Playgroud) java ×4
batch-file ×3
file ×2
jprogressbar ×2
swing ×2
android ×1
ant ×1
arrays ×1
bindservice ×1
build ×1
bytearray ×1
copy ×1
delimiter ×1
joptionpane ×1
optimization ×1
progress-bar ×1
sequential ×1
split ×1
string ×1
undefined ×1