📄 copier.java
字号:
/* * Copier.java * * Created on 12 janvier 2008, 02:11 */package copie;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;/** * * @author cadjak */public class Copier extends javax.swing.JFrame { static FileInputStream fist[] = new FileInputStream[9999]; static FileOutputStream fost[] = new FileOutputStream[9999]; static File[] lesfi; static java.nio.channels.FileChannel channelSrc = null ; static java.nio.channels.FileChannel channelDest= null ; //ProgressMonitorInputStream pmb[] = new ProgressMonitorInputStream[9999] ; /** Creates new form Copier */ public Copier() { initComponents(); jFrame1.setSize(this.getSize()); for(int i = 0;i<fist.length ; i++) { fist[i]=null; fost[i]=null; } info.setSize(400, 200); info.setLocationRelativeTo(this); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jFrame1 = new javax.swing.JFrame(); Destination = new javax.swing.JFileChooser(); info = new javax.swing.JDialog(); progress = new javax.swing.JProgressBar(); jLabel1 = new javax.swing.JLabel(); encours = new javax.swing.JLabel(); Source = new javax.swing.JFileChooser(); Destination.setDialogTitle("Destination"); Destination.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG); Destination.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY); Destination.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { DestinationActionPerformed(evt); } }); javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); jFrame1.getContentPane().setLayout(jFrame1Layout); jFrame1Layout.setHorizontalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 529, Short.MAX_VALUE) .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jFrame1Layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(Destination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) ); jFrame1Layout.setVerticalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 340, Short.MAX_VALUE) .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jFrame1Layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(Destination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) ); info.setTitle("Opération de copie en cours"); jLabel1.setText("Copie en cours :"); javax.swing.GroupLayout infoLayout = new javax.swing.GroupLayout(info.getContentPane()); info.getContentPane().setLayout(infoLayout); infoLayout.setHorizontalGroup( infoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(infoLayout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(encours, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap(250, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, infoLayout.createSequentialGroup() .addContainerGap(20, Short.MAX_VALUE) .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); infoLayout.setVerticalGroup( infoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(infoLayout.createSequentialGroup() .addGap(44, 44, 44) .addGroup(infoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(encours, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(33, 33, 33) .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(36, Short.MAX_VALUE)) ); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); Source.setDialogTitle("fichier à copier"); Source.setMultiSelectionEnabled(true); Source.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SourceActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 513, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(Source, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(Source, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) ); pack(); }// </editor-fold>//GEN-END:initComponents private void SourceActionPerformed(java.awt.event.ActionEvent evt) { try { // TODO add your handling code here: File[] fich = Source.getSelectedFiles(); lesfi = fich ; for(int i = 0;i<Source.getSelectedFiles().length ; i++) { fist[i]=new FileInputStream(fich[i]); } } catch (FileNotFoundException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } this.setVisible(false); jFrame1.setVisible(true); } private void DestinationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DestinationActionPerformed jFrame1.setVisible(false); info.setVisible(true); monThread mt = new monThread(); mt.start(); }//GEN-LAST:event_DestinationActionPerformed class monThread extends Thread { public monThread() { } @Override public void run() { try { // TODO add your handling code here: File l =Destination.getSelectedFile(); for(int i = 0;i<lesfi.length ; i++) { progress.setValue(0); String nomdest = l.getPath()+"\\"+lesfi[i].getName(); encours.setText(lesfi[i].getName()); fost[i]=new FileOutputStream(nomdest); channelSrc = fist[i].getChannel(); channelDest = fost[i].getChannel(); long lg = channelSrc.size(); if(!lesfi[i].isDirectory()) { long div = lg/100 ; long fin = div ; long deb = 0 ; for(int h = 1 ; h<=100 ; h++) { if(h<100) { channelSrc.transferTo(channelDest.position(), fin, channelDest); fin = fin + div ; progress.setValue(h); } else { channelSrc.transferTo(channelDest.position(), lg-channelDest.position(), channelDest); progress.setValue(h); } } } else { channelSrc.transferTo(0, channelSrc.size(), channelDest); } } } catch (IOException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } System.exit(1); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new Copier().setVisible(true); } catch (ClassNotFoundException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(Copier.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JFileChooser Destination; private javax.swing.JFileChooser Source; private javax.swing.JLabel encours; private javax.swing.JDialog info; private javax.swing.JFrame jFrame1; private javax.swing.JLabel jLabel1; private javax.swing.JProgressBar progress; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -