2012-04-01 4 views
1

자바에서 한 위치에서 다른 위치로 파일을 복사하려고합니다. 복사하려면 FileInputStreamFileOutputStream을 사용하고 있습니다. 여기 내 코드는 다음과 같습니다.파일을 복사하는 동안 ProgressMonitorInputStream을 사용하는 방법?

private JPanel contentPane; 
private JButton btnCopy; 
private JFileChooser SelectFile; 
private JFileChooser ChooseDestination; 
private File Selectedfile; 
private File chosenDestination; 


    protected void Copy() { 
     // TODO Auto-generated method stub 
     int answer = SelectFile.showOpenDialog(this); 
     if(answer==0){ 
      this.Selectedfile = SelectFile.getSelectedFile(); 
      int answer2 = ChooseDestination.showSaveDialog(this); 
      if(answer2==0){ 
       this.chosenDestination = ChooseDestination.getSelectedFile(); 
       try { 
        FileInputStream Read = new FileInputStream(Selectedfile); 
        FileOutputStream write = new FileOutputStream(chosenDestination + Selectedfile.getName()); 
        InputStream in = new BufferedInputStream(new ProgressMonitorInputStream(main.this,"Copying", new FileInputStream(Selectedfile))); 
        int Data; 
        byte[] buffer = new byte[1024]; 
        while((Data= in.read(buffer))>0){ 
         write.write(buffer); 
        }//Here the progressmMonitorInputStream window opens but there is nothing in the window. 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

파일 복사가 완료 될 때까지 계속됩니다. 그러나 파일이 올바르게 복사됩니다. 그냥 진도 표시 줄별도 창에 표시하고 싶습니다. 제발 도와주세요. 고맙습니다.

+1

당신은 (http://docs.oracle.com/javase/tutorial/uiswing/components/progress [진행률]의 사용에 대한 튜토리얼을 읽을 수 있습니다. html) 및 [Swing Worker 사용] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)을 참조하십시오. 코드에서 스윙의 스레딩 규칙을 고려하지 않기 때문입니다. –

답변

4

다음 코드는 도움이 될 것이다 :

import java.awt.BorderLayout; 

public class Main extends JFrame implements PropertyChangeListener { 

    private JPanel contentPane; 
    private JButton btnCopy; 
    private JFileChooser selectFile; 
    private JFileChooser chooseDestination; 
    private File selectedfile; 
    private File chosenDestination; 
    private JProgressBar progressBar; 
    private Copy copy; 

    class Copy extends SwingWorker<Void, Void> { 
     @Override 
     protected Void doInBackground() throws Exception { 
      int answer = selectFile.showOpenDialog(Main.this); 
      if (answer == 0) { 
       Main.this.selectedfile = selectFile.getSelectedFile(); 
       int answer2 = chooseDestination.showSaveDialog(Main.this); 
       if (answer2 == 0) { 
        Main.this.chosenDestination = chooseDestination 
          .getSelectedFile(); 
        try { 
         FileInputStream fileInputStream = new FileInputStream(
           selectedfile); 
         BufferedInputStream bufferedInputStream = new BufferedInputStream(
           fileInputStream); 
         ProgressMonitorInputStream progressMonitorInputStream = new ProgressMonitorInputStream(
           Main.this, "Copying", bufferedInputStream); 
         File outputFile = new File(chosenDestination 
           + selectedfile.getName()); 
         FileOutputStream fileOutputStream = new FileOutputStream(
           outputFile); 
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
           fileOutputStream); 
         int data; 
         byte[] buffer = new byte[1024]; 
         while ((data = progressMonitorInputStream.read(buffer)) > 0) { 
          bufferedOutputStream.write(buffer);       } 
         bufferedOutputStream.close(); 
         progressMonitorInputStream.close(); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
      return null; 
     } 

     /* 
     * Executed in event dispatching thread 
     */ 
     @Override 
     public void done() { 
      Toolkit.getDefaultToolkit().beep(); 
      btnCopy.setEnabled(true); 
      setCursor(null); // turn off the wait cursor 
     } 
    } 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Main frame = new Main(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    * 
    * @throws UnsupportedLookAndFeelException 
    * @throws IllegalAccessException 
    * @throws InstantiationException 
    * @throws ClassNotFoundException 
    */ 
    public Main() throws ClassNotFoundException, InstantiationException, 
      IllegalAccessException, UnsupportedLookAndFeelException { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 130, 135); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 
     progressBar = new JProgressBar(0, 100); 
     progressBar.setValue(0); 
     progressBar.setStringPainted(true); 
     JPanel panel = new JPanel(); 
     contentPane.add(panel, BorderLayout.CENTER); 
     panel.setLayout(new MigLayout("", "[89px][][][][][][][]", "[23px][][]")); 

     btnCopy = new JButton("Copy"); 
     btnCopy.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       copy = new Copy(); 
       copy.addPropertyChangeListener(Main.this); 
       copy.execute(); 
      } 
     }); 
     panel.add(btnCopy, "cell 1 1,grow"); 
     selectFile = new JFileChooser(); 
     chooseDestination = new JFileChooser(); 
     chooseDestination.setFileSelectionMode(2); 
     pack(); 
    } 

    @Override 
    public void propertyChange(PropertyChangeEvent evt) { 
     if ("progress" == evt.getPropertyName()) { 
      int progress = (Integer) evt.getNewValue(); 
      progressBar.setValue(progress); 
     } 
    } 
} 
+0

진행 모니터를 실제로 표시 할 때 ProgressMonitorInputStream을 이벤트 발송 스레드에서 작성해야합니까, 아니면 Java가 처리합니까? – joshden

관련 문제