2012-08-28 4 views
0

백업 프로그램에서 JFileChooser을 사용하여 선택한 폴더를 가져 와서 같은 방법으로 선택한 대상에 복사하려고합니다.동일한 대상의 대상에 폴더 복사

유일한 문제는 선택한 폴더의 모든 콘텐츠를 같은 이름의 대상 폴더에 넣지 않는다는 것입니다. 실제로 그 이유를 모르겠습니다. 나는 인터넷 검색을했고, 유용한 것을 찾지 못했습니다. 여기

코드입니다 :

나는 문제가 어떻게 생각
package main; 

import java.io.File; 
import java.util.ArrayList; 

public class test { 

BackgroundWorker bw; 
static ArrayList bgWorker = new ArrayList(); 
ArrayList al = new ArrayList(); // this is the list of files selected to 
           // back up 
String dir = ""; // this is the path to back everything up to selected by 
        static // the user 
boolean bwInitiallized = false; 

public void startBackup() throws Exception { 
    Panel.txtArea.append("Starting Backup...\n"); 

    for (int i = 0; i < al.size(); i++) { 
     /** 
     * THIS IS WHERE I NEED TO CREATE THE FOLDER THAT EACH BACKUP FILE 
     * WILL GO INTO EX: SC2 GOES INTO A FOLDER CALLED SC2 AND RIOT GOES 
     * TO RIOT, ALL WITHIN THE DIRECTORY CHOSEN 
     */ 
     File file = new File((String) al.get(i)); 
     File directory = new File(dir); 

     // File dirFile = new File(dir + "\\" + file.getName()); 
     // if (!dirFile.exists()) 
     // dirFile.mkdir(); 

     bw = new BackgroundWorker(Panel.txtArea, file, directory); 
     bgWorker.add(bw); 
     bwInitiallized = true; 
     bw.execute(); 

     /** 
     * follows to the bottom of the txtarea 
     */ 
     int x; 
     Panel.txtArea.selectAll(); 
     x = Panel.txtArea.getSelectionEnd(); 
     Panel.txtArea.select(1, x); 

    } 
    clearList(); // method not included in this example that deletes all the 
        // contents of the al array list. 
} 

public static void cancel() { 
    BackgroundWorker bg; 
    if (bwInitiallized) { 
     bwInitiallized = false; 
     Panel.txtArea.append("Cancelling...\n"); 
     for (int i = 0; i < bgWorker.size(); i++) { 
      // BackgroundWorker bg = (BackgroundWorker) bgWorker.get(i); 
      bg = (BackgroundWorker) bgWorker.get(i); 
      bg.cancel(true); 
     } 
     Panel.txtArea.append("Canceled backUp!\n"); 
    } else { 
     Panel.txtArea.append("Cannot Cancel! Not Initiallized!\n"); 
    } 
} 
} 

: 나는 어떤 이유에서 대상 파일 경로가 포함 된 폴더의 이름을 가질 필요가 있다고 생각하지만, 나는 그것을 시도하고 일부러 도움.

내가 뭘 잘못하고 있는지 아는 사람이 있습니까? 내가 누락 필요가 코드의

public void fileChooserToDestination() { 
    LookAndFeel previousLF = UIManager.getLookAndFeel(); 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch (Exception e) { 
    } 
    JFileChooser jfc = new JFileChooser(); 
    try { 
     UIManager.setLookAndFeel(previousLF); 
    } catch (UnsupportedLookAndFeelException e) { 
    } 

    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 

    if (jfc.showDialog(null, "Select Directory") == JFileChooser.APPROVE_OPTION) { 
     File file = jfc.getSelectedFile(); 
     dir = file.getPath(); 
     Panel.txtArea.append("User selected " + file.getPath() 
       + " for the destination...\n"); 
     try { 
      startBackup(); 
     } catch (Exception e) { 
     } 

    } else { 
     Dialogs.msg("You canceled selecting a destination folder! Returning to main screen..."); 
     al.clear(); 
     Panel.txtArea.append("User cancelled the destination selection..." 
       + "\n"); 
    } 

    return; 
} 
+1

'dir'에 대한 값을 결정하는 코드가 실제로 필요합니다. – MadProgrammer

+0

편집하십시오. – PulsePanda

답변

1

부품 :

JFileChooser를 만드는 코드입니다. 당신은 제거 할 필요가 기본적으로 ...는 점을 설명

File sourcePath = new File("/path/to/be/backed/up"); 
File destPath = new File("X:/BackupHere"); 

// Get all the files from sourcePath 
List<File> listFiles = getFilesFrom(sourcePath); 

for (File toBackup : listFiles) { 

    // Now we need to strip off the sourcePath 
    // Get the name of the file 
    String fileName = toBackup.getName(); 
    // Get parent folder's path 
    String path = toBackup.getParent(); 
    // Remove the source path from file path 
    path = path.substring(sourcePath.getPath().length()); 

    // Append the file name to the path 
    path = path + File.separator + fileName; 

    // Now we have the name of the back up file 
    String backupFile = destPath + path; 

    System.out.println("Backup to " + backupFile); 

} 

당신이 목적지 경로에 소스 파일을 추가하는 방법에 대한 의사 결정을하고 어디서 볼 수 없습니다, 그래서 나는이 간단한 예를 썼다 "원본 경로"(복사 할 디렉터리)의. 그런 다음 결과 값을 사용하여 "백업 경로"값에 추가하면 적절한 경로를 가져야합니다.

관련 문제