2013-08-02 2 views
0

나는 내 PC에서 주어진 확장자를 가진 파일을 검색 할 프로그램을 작성합니다. 이제는 한 가지 더 추가하고 싶습니다. 내 프로그램에서이 파일을 내 PC의 특정 위치로 복사 해 주시기 바랍니다. 당신이 파일을 복사 할 당신이, 당신은 위치에 출력 FileOutputStream의를 만들 수 있습니다 복사 할 파일에 대한 FileInputStream의이 있으면검색된 파일을 특정 위치에 복사하십시오.

Finder(String pattern) 
    { 
     matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); 
    } 

    // Compares the pattern against 
    // the file or directory name. 
    void find(Path file) { 
     Path name = file.getFileName(); 
     if (name != null && matcher.matches(name)) { 
      System.out.println(file); 
      String s = new String(name.toString()); 
      //System.out.println(s); 
      File f = new File(s); 
      //System.out.println(f.getAbsolutePath()); 
      FileInputStream fileInputStream = null; 
+0

여기서 정확히 질문은 무엇입니까? – Matthias

+0

체크 [이 스레드] (http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java) –

+0

나는 내 모든 텍스트 파일을 복사하려면 내 pendrive에 PC. 내 프로그램이 성공적으로 모든 텍스트 파일을 검색하고 지금은이 복사하고 싶습니다 .. 어떻게이 일을 할 수 있습니까 ?? – Vicky

답변

0

- : 여기 내 코드 샘플입니다. 그런 다음 파일을 복사하려면 다음과 같이 루프를 사용

byte temp; 
while ((temp = fileInputStream.read()) != -1) 
    fileOutputStream.write(temp); 
0

FileVistitor 인터페이스를 사용하여 파일 트리를 탐색하는 새로운 방법이 있습니다. here을 클릭하십시오.

0

아무 문제가 없습니다

public static void copyFile(File sourceFile, File newDirectory) throws IOException { 
    File destFile = new File(newDirectory, sourceFile.getName()); 
    if(!destFile.exists()) { 
     destFile.createNewFile(); 
    } 

    FileChannel source = null; 
    FileChannel destination = null; 
    try { 
     source = new FileInputStream(sourceFile).getChannel(); 
     destination = new FileOutputStream(destFile).getChannel(); 
     destination.transferFrom(source, 0, source.size()); 
    } 
    finally { 
     if(source != null) { 
      source.close(); 
     } 
     if(destination != null) { 
      destination.close(); 
     } 
    } 
} 

당신이 매개 변수에 너무 "newDirectory를"파일을 이동하려는 디렉토리를 정의 : 당신의 기준과 일치하는 파일에 접근 한 후 일부 높은 performanced 새로운 IO를 사용하여 이동 너의 질문!

  • 사용 java.nio.file.Files.copy() 방법 : 당신이 파일을 복사하는 방법을 묻는다면 , 당신은 옵션이 있습니다. 다른 답변에 의해 제안 된 데이터를 직접 이것은 당신이 경로를 사용하여 데이터를 직접 복사하지 않기 때문에 선호되고 그것은 표준 라이브러리에
  • 를 사용하여 스트림을 복사

  • 에 명령을 호출과 같은 다른 방법이 있습니다 복사 할 시스템

관련 문제