读取文章并显示进度

 import   javax.swing.*;  
  import   java .awt.*;  
  import   java.awt.event.*;  
  import   java.io.*;  
   
  public   class   DidoleoPM   implements   ActionListener,Runnable{  
   
  JFrame   f=null;  
  JLabel   label=null;  
  JTextArea   textarea=null;  
  JFileChooser   fileChooser=null;  
   
  public   DidoleoPM(){  
  f=new   JFrame("");  
  Container   contentPane=f.getContentPane();  
  textarea=new   JTextArea();  
  JScrollPane   scrollPane=new   JScrollPane(textarea);  
  scrollPane.setPreferredSize(new   Dimension(350,350));  
  JButton   b=new   JButton("读取文件");  
  b.addActionListener(this);  
  label=new   JLabel("",JLabel.CENTER);  
   
  fileChooser=new   JFileChooser();  
  contentPane.add(label,BorderLayout.NORTH);  
  contentPane.add(scrollPane,BorderLayout.CENTER);  
  contentPane.add(b,BorderLayout.SOUTH);  
   
  f.pack();  
  f.setVisible(true);  
   
  f.addWindowListener(new   WindowAdapter()  
  {  
  public   void   windowClosing(WindowEvent   e){  
  System.exit(0);  
  }  
  }  
   
  );  
   
  }  
   
   
  public   static   void   main(String[]   args){  
  new   DidoleoPM();  
  }  
   
  Thread   athread;  
   
  public   void   actionPerformed(ActionEvent   e){  
  athread=new   Thread(this);  
  athread.start();  
  }  
   
  public   void   run(){  
   
  File   file=null;  
  int   result=fileChooser.showOpenDialog(f);  
   
  textarea.setText("");  
   
   
  if(result==JFileChooser.APPROVE_OPTION){  
  file=fileChooser.getSelectedFile();  
  label.setText("你选择的文件名称为"+file.getName());  
  }else   if(result==JFileChooser.CANCEL_OPTION){  
  label.setText("你还没有选择任何文件");  
  }  
   
  FileInputStream   inputStream=null;  
   
  if(file!=null){  
  try{  
  inputStream=new   FileInputStream(file);  
  }catch(FileNotFoundException   fe){  
  label.setText("file   not   found");  
  return;  
  }  
  }  
   
  ProgressMonitorInputStream   pmInputStream=new  
  ProgressMonitorInputStream(f,"get   file...",inputStream);  
  ProgressMonitor   pMonitor=  
  pmInputStream.getProgressMonitor();  
  pMonitor.setMillisToDecideToPopup(10);  
  pMonitor.setMillisToPopup(0);  
  int   readbyte;  
   
  try{  
  while((readbyte=pmInputStream.read())!=-1){  
  textarea.append(String.valueOf((char)   readbyte));  
  try{  
  Thread.sleep(10);  
  }catch(InterruptedException   ie){  
   
  }  
   
  if(pMonitor.isCanceled()){  
  textarea.append("/n/n读取文件中断");  
  }  
  }  
   
  }catch(IOException   ioe){  
  label.setText("读取文件错误");  
  }finally{  
  try{  
  if(pmInputStream!=null){  
  pmInputStream.close();  
  }  
  }catch(IOException   ioe2){  
   
  }  
  }  
   
  }  
   
  }   
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值