2012-10-20 99 views
1

我想要读取数组中的元素并将读取的进度显示为并行过程读取。如何在IO操作期间显示进度

我有两个INTERAL类:

public class NumbersCounter { 

    int totalCountOfLines; 
    int currentCountOfLines = 1; 

    public NumbersCounter() throws FileNotFoundException, IOException {  
     new Read(); 
     new Progress(); 
    } 

    public static void main(String[] args) { 
     try { 
      new NumbersCounter(); 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(NumbersCounter.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(NumbersCounter.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    class Progress extends Thread { 
     public Progress() { 
      start(); 
     } 

     @Override 
     public void run() {   
      synchronized(new Object()) { 
       while (currentCountOfLines <= totalCountOfLines) { 
        System.out.println(currentCountOfLines/totalCountOfLines * 100 + "%"); 
        Thread.yield(); 
       }    
      } 
    }} 

    class Read extends Thread { 
     private FileHandler fh = new FileHandler("ololo.txt"); 
     private String[] lines; 

     public Read() throws FileNotFoundException, IOException { 
      this.lines = fh.readFromFile(); 
      start(); 
     } 

     @Override 
     public void run() { 

      totalCountOfLines = this.lines.length; 

      if (totalCountOfLines > 0) { 
       synchronized(new Object()) { 
        for (String line : lines) { 
         currentCountOfLines++; 
         Thread.yield(); 
        } 

       } 
      } else { 
       totalCountOfLines = 0; 
      }  
     } 
    } 
} 

当执行读取线程的第一步,我需要给执行到进度线程,然后为阅读,然后进展。

回答

1

使用javax.swing.ProgressMonitorInputStream类作为输入流的包装,该包装显示进度对话框。