java的流传输的进度条_JAVA程序设计(17)----- 制作文件拷贝软件 进程 输入流输出流 NIO 进度条 底层拷贝 多线程...
使用NIO對文件進行底層拷貝(按照字節)多線程技術初級應用 不阻塞程序運行
package com.lovo.homework01;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
/**
* 類:文件拷貝
* @author Abe
* NIO應用,JProgressBar應用
*/
@SuppressWarnings("serial")
public class NIOTest extends JFrame {
private JProgressBar jbar = null;
private int totleSize = 0;
private int copyedSize = 0;
private int eachSize = 0;
private JButton startButton = null;
private JTextField inField = null, outField = null;
/**
* 構造器
*/
public NIOTest() {
this.setSize(400, 300);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
/**
* JProgressBar設置
*/
jbar = new JProgressBar();
jbar.setMinimum(0);
jbar.setMaximum(100);//顯示上下限
jbar.setValue(0);//初始值
jbar.setStringPainted(true);//顯示百分比
jbar.setBounds(50, 150, 300, 30);
/**
* 文字輸入框
*/
inField = new JTextField();
inField.setBounds(50, 50, 300, 30);
outField = new JTextField();
outField.setBounds(50, 100, 300, 30);
/**
* 拷貝開始按鈕
*/
startButton = new JButton("開始拷貝");
startButton.setBounds(150, 200, 100, 30);
/**
* 按鈕加監聽器,匿名內部類就地實例化
*/
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startButton.setEnabled(false);
/**
* 多線程技術 新建一個線程 就地實例化Runnable接口
*/
new Thread(new Runnable() {
@Override
public void run() {
//新建輸入輸出流 這里用Filexxxxxx 是因為NIO的方法他們才能用
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(inField.getText());
out = new FileOutputStream(outField.getText());
//NIO 專屬 傳輸通道
FileChannel fcin = in.getChannel();
FileChannel fout = out.getChannel();
totleSize = in.available();
//NIO 專屬 運輸小車
ByteBuffer buffer = ByteBuffer.allocate(4096);
while ((eachSize = fcin.read(buffer)) != -1) {
//指針回零 然后開始讀取數據 直到內容末尾
buffer.flip();
fout.write(buffer);
//清空小車
buffer.clear();
//設置 進度條顯示內容
copyedSize += eachSize;
jbar.setValue((int) (100.0 * copyedSize / totleSize));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉輸入和輸出流
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();//千萬不要忘了 新建的線程 用這個開始運行
}
});
this.add(inField);
this.add(outField);
this.add(startButton);
this.add(jbar);
}
/**
* main方法 設置窗口可見
* @param args
*/
public static void main(String[] args) {
new NIOTest().setVisible(true);
}
}
總結
以上是生活随笔為你收集整理的java的流传输的进度条_JAVA程序设计(17)----- 制作文件拷贝软件 进程 输入流输出流 NIO 进度条 底层拷贝 多线程...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle清空无效数据,如何清除编译后
- 下一篇: A* 寻路 +寻路演示(js)