Java文件进度条
我是 Java 新手。 我想编写一个程序来生成文件的arcive 我想使用进度条来显示压缩进度 有我的代码,但它不起作用 工作完成后仅显示 100%。
getContentPane().add(progressBar);
progressBar.setBorder(new CompoundBorder(null, null));
springLayout.putConstraint(SpringLayout.EAST, progressBar, 0, SpringLayout.EAST, messageLabel);
springLayout.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, messageLabel);
springLayout.putConstraint(SpringLayout.SOUTH, progressBar, 165, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, progressBar, 5, SpringLayout.SOUTH, uploadLocalButton);
progressBar.setValue(0);
progressBar.setMaximum(100);
progressBar.setMinimum(0);
progressBar.setStringPainted(true);
...
public void doZip(String filename,String outFilename)
{
try {
byte[] dataBytes = new byte[1024];
File file = new File(outFilename);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
ZipOutputStream zos = new ZipOutputStream(fos);
File fil = new File(filename); //"Users/you/image.jpg"
long length= fil.length();
System.out.println(length);
zos.putNextEntry(new ZipEntry(fil.getName()));//image.jpg
FileInputStream in = new FileInputStream(filename);
int current=0;
int len;
while ((len = in.read(dataBytes)) > 0)
{ current += len;
messageLabel.setText(Integer.toString(current));
System.out.println(current);
final double progres=(((double)current/(double)length)*100);
progressBar.setValue((int)progres);
zos.write(dataBytes, 0, dataBytes.length);
}
zos.closeEntry();
zos.close();
in.close();
}catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Access denied. File or patch not exist or in currently in use.","Error",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}}
也许有人知道问题出在哪里?
我通过按下按钮来调用doZip。 我尝试了这个
while ((len = in.read(dataBytes)) > 0)
{ current += len;
zos.write(dataBytes, 0, dataBytes.length);
final double progres=(((double)current/(double)length)*100);
try{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
progressBar.setValue((int)progres);
}
});
java.lang.Thread.sleep(100);
}catch(InterruptedException e){;}
,但它也不起作用,只会使压缩操作过程变得更长。
I'm new to Java.
I want to write a program that makes arcive of file
and i want to use progress bar to show progress of zipping
There is my code, but it duosen't work
shows only 100% when work is done.
getContentPane().add(progressBar);
progressBar.setBorder(new CompoundBorder(null, null));
springLayout.putConstraint(SpringLayout.EAST, progressBar, 0, SpringLayout.EAST, messageLabel);
springLayout.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, messageLabel);
springLayout.putConstraint(SpringLayout.SOUTH, progressBar, 165, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.NORTH, progressBar, 5, SpringLayout.SOUTH, uploadLocalButton);
progressBar.setValue(0);
progressBar.setMaximum(100);
progressBar.setMinimum(0);
progressBar.setStringPainted(true);
...
public void doZip(String filename,String outFilename)
{
try {
byte[] dataBytes = new byte[1024];
File file = new File(outFilename);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
ZipOutputStream zos = new ZipOutputStream(fos);
File fil = new File(filename); //"Users/you/image.jpg"
long length= fil.length();
System.out.println(length);
zos.putNextEntry(new ZipEntry(fil.getName()));//image.jpg
FileInputStream in = new FileInputStream(filename);
int current=0;
int len;
while ((len = in.read(dataBytes)) > 0)
{ current += len;
messageLabel.setText(Integer.toString(current));
System.out.println(current);
final double progres=(((double)current/(double)length)*100);
progressBar.setValue((int)progres);
zos.write(dataBytes, 0, dataBytes.length);
}
zos.closeEntry();
zos.close();
in.close();
}catch (IOException e) {
JOptionPane.showMessageDialog(null,
"Access denied. File or patch not exist or in currently in use.","Error",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}}
Maybe someone knows where is the problem?
I call doZip by pressing the botton.
I tried this
while ((len = in.read(dataBytes)) > 0)
{ current += len;
zos.write(dataBytes, 0, dataBytes.length);
final double progres=(((double)current/(double)length)*100);
try{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
progressBar.setValue((int)progres);
}
});
java.lang.Thread.sleep(100);
}catch(InterruptedException e){;}
but it also doesent work, only makes process of zip opperation much more longer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在从 AWT 线程中的某个位置调用
doZip
(例如,由操作处理程序直接调用(传入)作为对单击或其他操作的响应)。因此,即使您在操作(压缩)完成之间相应地设置了进度,UI 也不会更新。一种解决方案是在单独的线程中启动
doZip
(一般来说,这是好的)建议避免在 AWT 线程中进行任何昂贵的操作,但只执行基本的“操作处理”并从那里触发长时间运行的操作...)I assume you are calling
doZip
from somewhere in the AWT thread (e.g. called (in)directly by an action handler as response to a click or something). Therefore the UI is not updated until the action (zipping) is finished, even if you set the progress accordingly in between..One solution would be to start
doZip
in a separate thread (in general it is good advice to avoid any costly operation in the AWT thread, but only do basic 'action handling' and trigger long-running operations from there...)