2017-03-14 5 views
0

파일 및 디렉토리 복사와 관련하여 몇 가지 문제가 있습니다. 원본 경로와 대상 경로에 대한 단추와 텍스트 상자가 있습니다.같은 시간에 디렉토리 및 파일 선택

소스 경로를 클릭하면 디렉토리를 선택할 수 있으며 파일을 복사하여 대상 경로에 배치 할 수 있는지 궁금합니다.

이 내 코드 지금까지

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.FileDialog; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Label; 
//import org.eclipse.swt.graphics.Path; 
//import org.eclipse.swt.program.Program; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.wb.swt.SWTResourceManager; 
import org.eclipse.swt.widgets.DateTime; 
//import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.DirectoryDialog; 

//import java.io.BufferedInputStream; 
//import java.io.BufferedOutputStream; 
import java.io.File; 
//import java.io.FileInputStream; 
//import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.nio.file.Files; 
import java.nio.file.StandardCopyOption; 
//import java.util.ArrayList; 
//import java.util.Collection; 

//import javax.swing.SwingWorker; 
//import org.eclipse.swt.custom.CLabel; 

public class FortryApplication { 

    protected static Shell shell; 
    private static Text txtSource; 
    private static Text txtDestination; 
    private static Text txt1; 
    InputStream inStream = null; 
    OutputStream outStream = null; 
    public Display display; 

    /** 
    * Open the window. 
    */ 
    public void open() { 
     Display display = Display.getDefault(); 
     createContents(); 
     shell.open(); 
     shell.layout(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
    } 
    /** 
    * Create contents of the window. 
    */ 
    protected void createContents() { 
     shell = new Shell(); 
     shell.setSize(450, 400); 
     shell.setText("Connection Manager"); 

     Label lblNewLabel = new Label(shell, SWT.NONE); 
     lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL)); 
     lblNewLabel.setBounds(10, 0, 107, 25); 
     lblNewLabel.setText("Source File "); 

     Button btnSourceSearch = new Button(shell, SWT.NONE); 
     btnSourceSearch.setBounds(10, 31, 75, 25); 
     btnSourceSearch.setText("Search"); 


     btnSourceSearch.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 

       DirectoryDialog dlg = new DirectoryDialog(shell); 
       dlg.setFilterPath(txtSource.getText()); 
       dlg.setMessage("Select a source file to transfer"); 
       String dir = dlg.open(); 
       if (dir != null) { 
        txtSource.setText(dir); 
       } 
      } 
     }); 
     txtSource = new Text(shell, SWT.BORDER); 
     txtSource.setBounds(91, 31, 333, 25); 

     Label lblNewLabel_1 = new Label(shell, SWT.NONE); 
     lblNewLabel_1.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL)); 
     lblNewLabel_1.setBounds(10, 62, 107, 25); 
     lblNewLabel_1.setText("Destination File"); 

     Button btnDestinationSearch = new Button(shell, SWT.NONE); 
     btnDestinationSearch.setBounds(10, 93, 75, 25); 
     btnDestinationSearch.setText("Search"); 

     btnDestinationSearch.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE); 
       dialog.setFilterPath(txtDestination.getText()); 
       String dir = dialog.open(); 
       if(dir != null){ 
        txtDestination.setText(dir); 
       } 
      } 
     }); 
     txtDestination = new Text(shell, SWT.BORDER); 
     txtDestination.setBounds(91, 93, 333, 25); 

     Label lblDateRange = new Label(shell, SWT.NONE); 
     lblDateRange.setBounds(10, 147, 81, 15); 
     lblDateRange.setText("Date Range:"); 

     Button btnStartCopy = new Button(shell, SWT.NONE); 
     btnStartCopy.setBounds(111, 249, 75, 25); 
     btnStartCopy.setText("Start Copy"); 


     btnStartCopy.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       File srcFolder = new File(txtSource.getText()); 
       File destFolder = new File(txtDestination.getText()); 

       if(!srcFolder.exists()){ 
        MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK); 
        msgBox.setText("Information"); 
        msgBox.setMessage("Directory does not exist."); 
        msgBox.open(); 
       }else{ 
        try{ 
         copyFolder(srcFolder,destFolder); 
        }catch(IOException e1){ 
         e1.printStackTrace(); 
        } 
        txt1.append("Finished Copying.....\n"); 
        MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK); 
        msgBox.setText("Information"); 
        msgBox.setMessage("Done Copying..."); 
        msgBox.open(); 
       } 
      } 
     }); 


     Button btnCancel = new Button(shell, SWT.NONE); 
     btnCancel.setBounds(233, 249, 75, 25); 
     btnCancel.setText("Cancel"); 

     btnCancel.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 

      } 
     }); 

     Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
     label.setBounds(0, 301, 434, 2); 

     Label lblFrom = new Label(shell, SWT.NONE); 
     lblFrom.setBounds(48, 172, 55, 15); 
     lblFrom.setText("From"); 

     DateTime ddFrom = new DateTime(shell, SWT.NONE); 
     ddFrom.setBounds(48, 193, 119, 23); 

     Label label_1 = new Label(shell, SWT.NONE); 
     label_1.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL)); 
     label_1.setBounds(207, 190, 22, 25); 
     label_1.setText(":"); 

     DateTime ddTo = new DateTime(shell, SWT.NONE); 
     ddTo.setBounds(253, 193, 119, 23); 

     Label lblTo = new Label(shell, SWT.NONE); 
     lblTo.setText("To"); 
     lblTo.setBounds(253, 172, 55, 15); 

     txt1 = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI); 
     txt1.setBounds(0,301,434,59); 
     txt1.setEnabled(true); 
     txt1.setText("Progress\n"); 

     Label lblStatus = new Label(shell, SWT.NONE); 
     lblStatus.setBounds(6, 285, 55, 15); 
     lblStatus.setText("Status:"); 

    } 

    public static void copyFolder(File src, File dest) 
      throws IOException{ 
     if(src.isDirectory()){ 
      if (!dest.exists()) 
      { 
       dest.mkdir(); 
       txt1.append("Directory created : " + dest + "\n"); 
      } 
      String files[] = src.list(); 
      for (String file : files) 
      { 
       File srcFile = new File(src, file); 
       File destFile = new File(dest, file); 

       //Recursive function call 
       copyFolder(srcFile, destFile); 
      } 
     } 
     else{ 
      Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); 
      txt1.append("Copying " + src.getAbsolutePath() + "\n"); 

     } 
//  if (src.exists()) { 
//   src.delete(); 
//  } 
    } 
    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String[] args) { 

     try { 
      FortryApplication window = new FortryApplication(); 
      window.open(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

PS입니다 : 내가 SWT와 WindowsBuilder을 사용하고 있는데 난 단지 파일을 디렉토리를 선택하고 수 없습니다.

답변

0

DirectoryDialog

예 사용하는 대신 FileDialog를 살펴 보자 :

FileDialog dialog = new FileDialog(shell, SWT.OPEN); 
    dialog.setFilterExtensions(new String [] {"*.html"}); 
    dialog.setFilterPath("c:\\temp"); 
    String result = dialog.open(); 

FAQ How do I prompt the user to select a file or a directory?

+0

내가 그 시도 만 파일을 하나 복사, 내가 예를 들어 모두를 원하는 나는 검색 버튼을 클릭하고 창을 띄우며 디렉토리와 파일을 보여줍니다. 폴더를 클릭 할 수도 있고 파일도 가져올 수 있습니까? 미안 나는 영어로 표현할 수 없다. – jowdislove

관련 문제