1. 程式人生 > >利用ProgressMonitorInputStream進行輸入流的進度視窗監視

利用ProgressMonitorInputStream進行輸入流的進度視窗監視

import java.awt.Container;
import java.io.*;

import javax.swing.*;

public class Student {
	public static void main(String[] temp) {
		byte b[] = new byte[2];
		try{
			FileInputStream fis = new FileInputStream("word.txt");
			JFrame jf=new JFrame();
			jf.setLayout(null);
			Container container=jf.getContentPane();
			
			JTextArea jta=new JTextArea();
			JScrollPane jsp=new JScrollPane(jta);
			jsp.setBounds(0,0, 1200,300);
			
			container.add(jsp);
			jf.setTitle("test");
			jf.setVisible(true);
			jf.setSize(1300,800);
			jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			ProgressMonitorInputStream in = new ProgressMonitorInputStream(jf,"讀取檔案",fis);
		    while(in.read(b)!=-1){
			    String s = new String(b);
			    jta.append(s);
			    Thread.sleep(100);//設定讀取速度,100毫秒一次
		    }
		 }catch (Exception e) {
			e.printStackTrace();
		 }
	}
}