Java FileInputStream/FileOutputStream阻塞?
我有以下成功复制文件的代码。但是,它有两个问题:Java FileInputStream/FileOutputStream阻塞?,java,swing,event-dispatch-thread,jprogressbar,Java,Swing,Event Dispatch Thread,Jprogressbar,我有以下成功复制文件的代码。但是,它有两个问题: progressBar.setValue()后面的System.out.println()不打印0到100之间的间隔(只打印“0”,直到打印“100”为止) 除了由于问题#1,进度条的值可能以某种方式错误之外,在实际代码中,我也在进行其他视觉更改,但直到处理完整个文件后,它们才会显示出来。我认为FileInputStream/FileOutputStream函数是非阻塞的。如何更改以下代码,以便在操作过程中更新进度条 开始作业方法: privat
private void startJob(File inFile, File outFile) {
long offset = 0;
int numRead = 0;
byte[] bytes = new byte[8192];
long fileLength = inFile.length();
Boolean keepGoing = true;
progressBar.setValue(0);
try {
inputStream = new FileInputStream(inFile);
outputStream = new FileOutputStream(outFile, false);
System.out.println("Total file size to read (in bytes) : " + inputStream.available());
} catch (FileNotFoundException err) {
inputStream = null;
outputStream = null;
err.printStackTrace();
} catch (IOException err) {
inputStream = null;
outputStream = null;
err.printStackTrace();
}
if (inputStream != null && outputStream != null) {
while (keepGoing) {
try {
numRead = inputStream.read(bytes);
outputStream.write(bytes, 0, numRead);
} catch (IOException err) {
keepGoing = false;
err.printStackTrace();
}
if (numRead > 0) {
offset += numRead;
}
if (offset >= fileLength) {
keepGoing = false;
}
progressBar.setValue(Math.round(offset / fileLength) * 100);
System.out.println(Integer.toString(Math.round(offset / fileLength) * 100));
}
}
if (offset < fileLength) {
//error
} else {
//success
}
try {
inputStream.close();
outputStream.close();
} catch (IOException err) {
err.printStackTrace();
}
}
private void startJob(文件填充,文件输出){
长偏移=0;
int numRead=0;
字节[]字节=新字节[8192];
long fileLength=infle.length();
布尔值keepGoing=true;
progressBar.setValue(0);
试一试{
inputStream=新文件inputStream(infle);
outputStream=新文件outputStream(outFile,false);
System.out.println(“要读取的总文件大小(字节):”+inputStream.available());
}捕获(FileNotFoundException错误){
inputStream=null;
outputStream=null;
err.printStackTrace();
}捕获(IOException err){
inputStream=null;
outputStream=null;
err.printStackTrace();
}
if(inputStream!=null&&outputStream!=null){
同时(继续){
试一试{
numRead=inputStream.read(字节);
写入(字节,0,numRead);
}捕获(IOException err){
keepGoing=false;
err.printStackTrace();
}
如果(numRead>0){
偏移量+=numRead;
}
如果(偏移量>=文件长度){
keepGoing=false;
}
progressBar.setValue(数学舍入(偏移量/文件长度)*100);
System.out.println(Integer.toString(数学四舍五入(偏移量/文件长度)*100));
}
}
if(偏移量<文件长度){
//错误
}否则{
//成功
}
试一试{
inputStream.close();
outputStream.close();
}捕获(IOException err){
err.printStackTrace();
}
}
我怀疑您正在从中调用冗长的方法。例如,将操作置于EDT自己的Runnable
中,然后调用
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setValue(value);
// or any other GUI changes you want to make
}
});
否则,您的操作将阻止EDT,直到它完成为止,并且在EDT被阻止时,无法处理任何事件,如重新绘制等->在结束之前,没有可见的GUI更改。表达式
Math.round(offset/fileLength)
的值将始终等于0
(零),因为offset
UPD:
如果要正确执行此计算,必须将其更改为:
Math.round(((double)offset / (double)fileLength) * 100)
可能的副本您是否考虑过使用?这可以简化您的许多工作。FileInputStream怎么可能是非阻塞的,并且仍然返回已读取的内容?代码中的另一个问题(除了已经确定的所有其他问题)是,如果出现异常,您将尝试关闭空输入流。它说它们是非阻塞的是什么?