仅在SWT中复制文件而不是在jface中显示进度条

时间:2015-05-06 11:28:57

标签: java swt

我正在尝试在SWT中创建一个安装向导。

我有一些代码在复制文件时显示进度条。

 import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.RandomAccessFile;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.IOUtils;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.ControlEvent;
    import org.eclipse.swt.events.ControlListener;
    import org.eclipse.swt.events.MouseEvent;
    import org.eclipse.swt.events.MouseListener;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Font;
    import org.eclipse.swt.graphics.FontData;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.layout.FormAttachment;
    import org.eclipse.swt.layout.FormData;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.DirectoryDialog;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.MessageBox;
    import org.eclipse.swt.widgets.ProgressBar;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public class SWT_Second {

        public static Button b2;
        public static Composite directoryInstComposite;
        public static Composite workComposite;
        public static Composite copyFilesComposite;
        public static Label dir_entry_label;
        public static Text dir_entry_text;
        public static Label dir_info_label;
        public static Button dir_next_button;
        public static Button dir_cancel_button;
        public static Button copy_next_button;
        public static Button copy_cancel_button;        
        public static Shell shell;
        public static ProgressBar pbar;
        public static Display display;
        public static File currentDir;
        public static boolean copyFlag=false;
        public static boolean actualCopyFlag=false;
        public static boolean copySuccess=true;
        public static int runWithoutAdminRights=-1;

        public static Combo c1;

        public static void main(String[] args) {
                display = new Display();
            shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN |SWT.MAX| SWT.CENTER | SWT.SYSTEM_MODAL | SWT.
    APPLICATION_MODAL);  
            shell.setSize(450,348);
            shell.setLocation(250, 150);
            shell.setText("Installer!");
            shell.setLayout(new FormLayout());

            final Image HKimage0=new Image(display,SWT_Second.class.getResourceAsStream("Img/d.png"));
            shell.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(HKimage0, 0, 0);
                }
            });
            shell.setImage(HKimage0);
            Composite headerComposite = new Composite(shell, SWT.NONE);
            headerComposite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            FormData shellFormData = new FormData();

            shellFormData.top=new FormAttachment(0,0);
            shellFormData.left=new FormAttachment(00,0);

            shellFormData.width=1364;
            shellFormData.height=60;

            headerComposite.setLayoutData(shellFormData);
            Canvas prodName = new Canvas(headerComposite, SWT.NONE);
            prodName.setBounds(6,6, 135,50);
            prodName.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

            Canvas canvas = new Canvas(headerComposite, SWT.NONE);
            canvas.setSize(500,60);

            canvas.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,3,437, 56);
                }
            });
            final Image HKimage=new Image(display,SWT_Second.class.getResourceAsStream("Img/Logo_True_Colors.png"));

            prodName.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.drawImage(HKimage, 0, 0);
                }
            });

            workComposite = new Composite(shell, SWT.NONE);
            workComposite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

            FormData shellFormData1 = new FormData();
            shellFormData1.top=new FormAttachment(0,53);
            shellFormData1.left=new FormAttachment(00,0);
            shellFormData1.width=1364;
            shellFormData1.height=1000;
            workComposite.setLayoutData(shellFormData1);

            Canvas canvas1 = new Canvas(workComposite, SWT.NONE);
            canvas1.setSize(500,230);
            canvas1.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas1.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,10,437, 219);
                }
            });
            Canvas canvas2= new Canvas(workComposite, SWT.NONE);
            canvas2.setSize(500,280);
            canvas2.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas2.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {

                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,233,437, 31);

                }
            });

            final Label inst_wel_label = new Label(canvas1,SWT.NONE);
            inst_wel_label.setText("Welcome to Setup");
            Font boldFont = new Font( inst_wel_label.getDisplay(), new FontData( "TimesNewRoman", 16, SWT.BOLD ) );
            inst_wel_label.setFont( boldFont );

            inst_wel_label.setLocation(65, 50);
            inst_wel_label.setSize(245, 25);
            inst_wel_label.setAlignment(2);

            final Label inst_type_label = new Label(canvas1,SWT.NONE);
            inst_type_label.setText("Select Installation Type");
            inst_type_label.setLocation(65,100);
            inst_type_label.setSize(123, 25);
            inst_type_label.setAlignment(2);

            c1 = new Combo(canvas1, SWT.READ_ONLY);
            c1.setBounds(205,100, 130,65);
            c1.select(0);

            String items[] = { "Dev User", "Operations User"};
            c1.setItems(items);
            c1.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    if (c1.getText().equals("Dev User")) {
                        InstallUser="Dev User";
                    } else if (c1.getText().equals("Operations User")) {
                        InstallUser="Operations User";
                    } else {
                        InstallUser="NONE";
                    }
                }
            });
   final Label inst_start_label = new Label(canvas1,SWT.NONE);
            inst_start_label.setText("To start installation process, click Go");
            inst_start_label.setLocation(65, 140);
            inst_start_label.setSize(200,15);

            Button b = new Button(canvas2, SWT.NONE);
            b.setText("Go");
            b.setSize(50, 20);
            b.setLocation(310,240);

            Button b1 = new Button(canvas2, SWT.NONE);
            b1.setText("Cancel");
            b1.setSize(50, 20);
            b1.setLocation(365,240);

            b1.addMouseListener(new MouseListener() {
                @Override
                public void mouseUp(MouseEvent arg0) {
                }
                @Override
                public void mouseDown(MouseEvent arg0) {
                    System.exit(0);
                }
                @Override
                public void mouseDoubleClick(MouseEvent arg0) {
                }
            });
            i = 0;
            workComposite.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent arg0) {
                    System.out.println("Inside Paint Listener " + new Date());
                    if(i==0) {
                        i++;
                        Date dt = null;
                        SimpleDateFormat st = null;
                        File checkAdminRightsFile = null;
                        try {
                            dt = new Date();
                            st = new SimpleDateFormat("yyyyMMddHHmmss");
                            String dd = st.format(dt);
                            checkAdminRightsFile = new File("C:\\Installer".concat(dd));
                            RandomAccessFile checkAdminRights = new RandomAccessFile(checkAdminRightsFile, "rw");
                            checkAdminRights.close();
                            runWithoutAdminRights=SWT.NO;
                        }
                        catch (Exception fs) {
                            if(fs.getMessage().contains("Access is denied")) {
                                MessageBox DirMsgBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
                                runWithoutAdminRights = DirMsgBox.open();
                                if(runWithoutAdminRights==SWT.NO) {
                                    MessageBox DirMsgBox1 = new MessageBox(shell);
                                    DirMsgBox1.setText("Aborting Installation");
                                    DirMsgBox1.open();
                                    System.exit(0);
                                }
                            } else {
                                MessageBox DirMsgBox1 = new MessageBox(shell);
                                DirMsgBox1.setMessage("Encountered error while creating file " + 
    checkAdminRightsFile.getAbsoluteFile() +" : " + fs.getMessage() + "\n Aborting the 
    process for now");                  
                                DirMsgBox1.open();
                                System.exit(0);
                            }
                        }    
                        try {
                            checkAdminRightsFile.delete();
                        } catch (Exception ot) {
                            MessageBox DirMsgBox1 = new MessageBox(shell);
                            DirMsgBox1.setMessage("Encountered error while deleting the temp file " + 
    checkAdminRightsFile.getAbsoluteFile() +" : " + ot.getMessage() + "\n Aborting the process 
    for now");                  
                            DirMsgBox1.open();
                            System.exit(0);
                        }
                    }
                }
            });

            b.addMouseListener(new MouseListener() {
                @Override
                public void mouseUp(MouseEvent arg0) {
                }
                @Override
                public void mouseDown(MouseEvent arg0) {
                    if(c1.getSelectionIndex()==-1) {
                        MessageBox DirMsgBox = new MessageBox(shell);
                        DirMsgBox.setText("Please select Installation Type ");
                        DirMsgBox.setMessage("Installation type is not selected - please select to proceed ...");
                        DirMsgBox.open();
                    } else {
                        b2.setVisible(true);
                        workComposite.setVisible(false);
                        directoryInstComposite.setVisible(true);
                    }
                }
                @Override
                public void mouseDoubleClick(MouseEvent arg0) {
                    workComposite.setVisible(false);
                    directoryInstComposite.setVisible(true);
                }
            });

            directoryInstComposite = new Composite(shell, SWT.NONE);  
            directoryInstComposite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));    
            FormData FD_InstPath = new FormData();
            FD_InstPath.top=new FormAttachment(0,53);
            FD_InstPath.left=new FormAttachment(00,0);
            FD_InstPath.width=1364;
            FD_InstPath.height=1000;
            directoryInstComposite.setLayoutData(FD_InstPath);    
            directoryInstComposite.setVisible(false);

            Canvas canvas3 = new Canvas(directoryInstComposite, SWT.NONE);
            canvas3.setSize(500,230);
            canvas3.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas3.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,10,437, 219);
                }
            });
            Canvas canvas4= new Canvas(directoryInstComposite, SWT.NONE);
            canvas4.setSize(500,280);
            canvas4.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas4.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,233,437, 31);
                }
            });  
            dir_info_label = new Label(canvas3, SWT.NONE);
            dir_info_label.setText("Browse for Configuration Directory...");
            Font boldFont3 = new Font( dir_info_label.getDisplay(), new FontData( "TimesNewRoman", 10, SWT.BOLD ) );
            dir_info_label.setFont(boldFont3);
            dir_info_label.setSize(350, 19);
            dir_info_label.setLocation(75,25);

            dir_entry_label = new Label(canvas3, SWT.NONE);
            dir_entry_label.setText("Location:");
            dir_entry_label.setSize(60, 20);
            dir_entry_label.setLocation(18,65); 

            dir_entry_text = new Text(canvas3, SWT.BORDER );

            dir_entry_text.setText("Enter Directory");
            dir_entry_text.setSize(270,19);
            dir_entry_text.setLocation(75,65);
            dir_browse_button = new Button(canvas3, SWT.PUSH);
            dir_browse_button.setText("Browse...");
            dir_browse_button.setSize(54,19);
            dir_browse_button.setLocation(350,65);
            dir_browse_button.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent event) {
                    DirectoryDialog dlg = new DirectoryDialog(shell);
                    // Set the initial filter path according
                    // to anything they've selected or typed in
                    dlg.setFilterPath(dir_entry_text.getText());
                    System.out.println("dlg.setFilterPath: "+dir_entry_text.getText());
                    // Change the title bar text
                    dlg.setText("SWT's DirectoryDialog");
                    // Customizable message displayed in the dialog
                    dlg.setMessage("Select a directory");
                    // Calling open() will open and run the dialog. It will return the selected directory, or null if user cancels
                    String dir = dlg.open();
                    if (dir != null) {
                        // Set the text box to the new selection
                        dir_entry_text.setText(dir);
                    }
                }
            }); 
            dir_next_button = new Button(canvas4, SWT.NONE);
            dir_next_button.setSize(50, 20);
            dir_next_button.setLocation(310, 240);
            dir_next_button.setText("Install");

            dir_cancel_button = new Button(canvas4, SWT.NONE);
            dir_cancel_button.setSize(50, 20);
            dir_cancel_button.setLocation(365, 240);
            dir_cancel_button.setText("Cancel");

            dir_next_button.addMouseListener(new MouseListener() {

                @Override
                public void mouseDown(MouseEvent arg0) {
                    try {
                        if(dir_entry_text.getText()!=null && !dir_entry_text.getText().equalsIgnoreCase("")) {
                            BaseDir = new File(dir_entry_text.getText());
                            if(!BaseDir.exists() && !BaseDir.isDirectory()) {
                                MessageBox DirMsgBox = new MessageBox(shell);
                                DirMsgBox.setText("Directory does not exist!!!! ");         
                                DirMsgBox.open();                   
                            } else {
                                directoryInstComposite.setVisible(false);
                                copyFilesComposite.setVisible(true);
                                pbar.setVisible(true);
                                (new SWT_Second()).new ProgressBarClass(display, pbar).start(); 
                                System.out.println("Text in dir_entry_text is  : " + dir_entry_text.getText());

                                String workingDir = System.getProperty("user.dir");
                                System.out.println("Current directory is : " + workingDir);
                                currentDir = new File(System.getProperty("user.dir"));
                                try {
                                    logFile = new File(BaseDir.getAbsolutePath().concat(File.separator).concat(
    "Insaller.log"));
                                    if(logFile.exists()) {
                                        Date curr = new Date();
    ".").concat(curr.toString())));

                                    }                   
                                            } catch (Exception ex) {
                                    MessageBox DirMsgBox = new MessageBox(shell);
                                    DirMsgBox.setMessage("Exception while creating log file : " + ex.getMessage() + 
    "\n Aborting the process..");                   
                                    DirMsgBox.open();       
                                    System.exit(0);
                                // Remove this comment - commented for runnig from eclipse
                                //copyFiles(currentDir,BaseDir);
                                Thread t = new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        try {
                                            File SetupDir = new File(SWT_Second.currentDir.getAbsolutePath().concat(File
    .separator).concat("setup"));
                                            copyFiles(SetupDir,BaseDir);

                                            actualCopyFlag=true;
                                        } 
                                        catch (IOException io) {
                                            SWT_Second.actualCopyFlag=false;
                                            SWT_Second.copySuccess=false;


                                        } catch (Exception ex) {
                                            SWT_Second.actualCopyFlag=false;
                                            SWT_Second.copySuccess=false;
                                            ex.printStackTrace();
                                            MessageBox DirMsgBox = new MessageBox(shell);
                                            DirMsgBox.setText("Generic problem!!!! ");
                                            DirMsgBox.setMessage("Some problem occured .. aborting .. check logs 
    for details! \n" + ex.getMessage() );                   
                                            DirMsgBox.open();

                                            System.exit(0);
                                        }
                                    }
                                });
                                t.start();
                            }
                            System.out.println("Copy Progress bar is complete ");
                        } else {
                            MessageBox DirMsgBox = new MessageBox(shell);
                            DirMsgBox.setText("Please select a directory");
                            DirMsgBox.setMessage("Please select a directory to Install ..");                    
                            DirMsgBox.open();
                        }
                    }  catch (Exception gen) {
                    gen.printStackTrace();
                    MessageBox DirMsgBox = new MessageBox(shell);
                    DirMsgBox.setMessage("Some problem occured .. aborting .. check logs for details! \n" + gen.
    getMessage() );                 
                    DirMsgBox.open();

                }
                }
                @Override
                public void mouseDoubleClick(MouseEvent arg0) {

                    if(dir_entry_text.getText()!=null && !dir_entry_text.getText().equalsIgnoreCase("")) {

                    }           
                }
            });

            copyFilesComposite = new Composite(shell, SWT.NONE);     
            copyFilesComposite.setLayoutData(FD_InstPath);    
            copyFilesComposite.setVisible(false);

            Canvas canvas5 = new Canvas(copyFilesComposite, SWT.NONE);
            canvas5.setSize(500,230);
            canvas5.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas5.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,10,437, 219);
                }
            });
            Canvas canvas6= new Canvas(copyFilesComposite, SWT.NONE);
            canvas6.setSize(500,280);
            canvas6.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
            canvas6.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent e) {
                    e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
                    e.gc.drawRectangle(3,233,437, 31);
                }

            pbar = new ProgressBar(canvas5,SWT.NULL);
            pbar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            pbar.setMinimum(0);
            pbar.setMaximum(30);
            pbar.setLocation(100,90);
            pbar.setSize(280,14);

            copy_next_button = new Button(canvas6, SWT.NONE);
            copy_next_button.setSize(50, 20);
            copy_next_button.setLocation(310, 240);
            copy_next_button.setText("Next");       
            canvas6.addPaintListener(new PaintListener() {
                @Override
                public void paintControl(PaintEvent arg0) {
                    if(copySuccess) {
                        if(copyFlag && actualCopyFlag) {
                            copy_next_button.setEnabled(true);
                        } 
                    } else {
                        MessageBox DirMsgBox = new MessageBox(shell);
                        DirMsgBox.setText("Error in Copy Operation");
                        DirMsgBox.setMessage("Please check if the directory has write permissions and does not have 
    Files already present \n please delete the exisitng Files and retry \n Aborting now.."
     );                 
                        DirMsgBox.open();
                            System.exit(0);
                    }
                }
            });   
            public static void copyFiles(File srcDir, File destDir)  throws IOException{
            String strSubDestDir=null;
            File subDestDir=null;

            for (File f : srcDir.listFiles()) {
                if (f.isDirectory()) {
                    System.out.println("It is a directory");
                    strSubDestDir = destDir.getAbsolutePath().concat(File.separator).concat(f.getName());
                    subDestDir = new File(strSubDestDir);
                    if( !subDestDir.exists() ) {
                        subDestDir.mkdir();
                    }
                    copyFiles(f, subDestDir);
                } else {    
                    System.out.println("It is simple a file");
    Paths.get(f.getAbsolutePath()).toString() + " to "+  
    Paths.get(destDir.getAbsolutePath()).toString());
                    File newFile = new File(destDir.getAbsolutePath().concat(File.separator).concat(f.getName()));
                    count++;
                    System.out.println("COUNT : "+count);
                    if(f.getName().equalsIgnoreCase("Dt.jar") || f.getName().equalsIgnoreCase("ProgressViwer.jar")) {
                        if(f.getName().equalsIgnoreCase("Dt.jar") && InstallUser.equalsIgnoreCase("Dt Dev User")) {
                            Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(newFile.getAbsolutePath()), 
    StandardCopyOption.COPY_ATTRIBUTES);
                        } 
                        if (f.getName().equalsIgnoreCase("ProgressViwer.jar") && InstallUser.equalsIgnoreCase(
    "Operations User")) {
                            Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(newFile.getAbsolutePath()), 
    StandardCopyOption.COPY_ATTRIBUTES);

                        } 
                    } else {
                        Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(newFile.getAbsolutePath()), 
    StandardCopyOption.COPY_ATTRIBUTES);
                    }
                }
            }
        }
            class ProgressBarClass extends Thread {
            private Display display;
            private ProgressBar progressBar;
            public ProgressBarClass(Display display, ProgressBar progressBar) 
            {
                this.display = display;
                this.progressBar = progressBar;
            }
            public void run()
            {
                for (int i = 0; i < 30; i++) 
                {
                    try 
                    {
                        Thread.sleep(500);
                    } 
                    catch (InterruptedException e)
                    {
                    }
                    display.asyncExec(new Runnable() 
                    {
                        public void run() 
                        {
                            if (progressBar.isDisposed())
                            {                 
                                return;
                            }
                            System.out.println("progressBar.getSelection() is @@==> " + progressBar.
    getSelection());
                            progressBar.setSelection(progressBar.getSelection() + 1);
                            System.out.println("Completed");
                        }
                    });
                }
                copyFlag=true;       
            }
        }
    }

我的代码显示进度条,但文件未被复制。

我该如何解决这个问题?

编辑:

我正在尝试使用示例代码。

public static void main (String [] args) throws IOException 
    {
        Display display = new Display ();
        Shell shell = new Shell (display);

        shell.open ();
        File srcFile=new File("C:\\Users\\aa00333221\\Desktop\\s");
        File destFile=new File("C:\\Users\\aa00333221\\Desktop\\S2");

        String[] files=srcFile.list();
        System.out.println("files :"+files);

        f1=numberOfFiles(srcFile);
        System.out.println("Count: "+f1);

        ProgressBar bar = new ProgressBar (shell, SWT.SMOOTH);
        bar.setLocation(100,90);
        bar.setSize(280,14);
        bar.setMaximum(f1);

        for (String name : files) {
            System.out.println("File name: " + name);

        }

        for(int i=0;i<f1;i++){
            System.out.println("Files[i]: "+files[i]);
strSubDestDir = srcFile.getAbsolutePath().concat(File.separator).concat(files[i]);
            subDestDir = new File(strSubDestDir);
File newFile = new File(destFile.getAbsolutePath().concat(File.separator).concat(files[i]));
FileUtils.copyFile(subDestDir, newFile);
            bar.setSelection (i+1);
            int x=i+1;
            System.out.println("bar.setSelection (i+1): "+x );
            System.out.println("copy done for "+strSubDestDir +"to "+newFile);
        }
        while (!shell.isDisposed ())
        {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }

    public static int numberOfFiles(File srcDir) {
        int count = 0;
        System.out.println("calling NumbeerOfFiles Method");
        File[] listFiles = srcDir.listFiles();
        for(int i = 0; i < listFiles.length; i++){
            if (listFiles[i].isDirectory()) {
                count += numberOfFiles(listFiles[i]);
            } else if (listFiles[i].isFile()) {
                count++;
            }
        }
        return count;
    }
This is working for files only, not for subfolders.
Have to display the progress bar while copying folders and subfolders also.

1 个答案:

答案 0 :(得分:1)

static ProgressBar bar;
    static int pb=0;
public static void copyFiles(final File srcFile,File destFile)  throws IOException{
        String mkDir= null;
        File mkDir1=null;
        String cpDir= null;
        File cpDir1=null;


        System.out.println("INSIDE METHOD :");

        {
            z=srcFile.listFiles();
            System.out.println("INSIDE FIRST LOOP");

            for (File f : srcFile.listFiles()) {
                if (f.isFile()) {
                    pb++;
                    System.out.println("File name ZZ: "+f.getName());
                    String sSrcDir=srcFile.getAbsolutePath().concat(File.separator).concat(f.getName());
                    System.out.println("sSrcDir: "+sSrcDir);
                    File sSrcFile=new File(sSrcDir);

                    File newFile = new File(destFile.getAbsolutePath().concat(File.separator).concat(f.getName()));
                    System.out.println("newFile: "+newFile);

                    FileUtils.copyFile(sSrcFile, newFile);
                    bar.setSelection (pb+1);
                    int x=pb+1;
                    System.out.println("bar.setSelection (j+1): "+x );
                    System.out.println("copy done for "+strSubDestDir +"to "+newFile);
                    //FileUtils.copyFile(srcFile, destFile);
                }
                else if(f.isDirectory())
                {
                    System.out.println("INSIDE DIRECTORY LOOP");
                    System.out.println("It is a directory"+f.getName());
                    cpDir=srcFile.getAbsolutePath().concat(File.separator).concat(f.getName());
                    cpDir1=new File(cpDir);
                    System.out.println("cpDir:  "+cpDir);
                    mkDir = destDir.getAbsolutePath().concat(File.separator).concat(f.getName());

                    mkDir1 = new File(mkDir);
                    System.out.println("mkDir : "+mkDir);
                    if( !mkDir1.exists() ) {
                        mkDir1.mkdir();
                    }
                    copyFiles(cpDir1, mkDir1);
                    //FileUtils.copyDirectory(cpDir1, mkDir1);
                }
            }
        }
    }