2012-03-28 13 views
-1

ここにコードがあります。以下のJavaコードで何が問題になっていますか?

import java.nio.file.*; 
import java.io.*; 

public class FileCopy{ 

    public static void main(String[] args){ 

     String sourcePath=new String(); 
     String targetPath=new String(); 
     StreamTokenizer token=new StreamTokenizer(
        new BufferedReader(   
         new InputStreamReader(System.in))); 
     int type=0; 
     token.wordChars('*','*'); 
     token.wordChars(':',':'); 
     token.wordChars('/','/'); 
     token.wordChars('.','.'); 

     try{ 

      System.out.print("Please type in the source path:"); 
      if((type=token.nextToken())==StreamTokenizer.TT_WORD) 
       sourcePath=token.sval; 
      System.out.print("Please type in the target path:"); 
      if((type=token.nextToken())==StreamTokenizer.TT_WORD) 
       targetPath=token.sval; 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 

     Path source=Paths.get(sourcePath); 
     Path target=Paths.get(targetPath); 
     System.out.println("Please enter to create the sourcePath and the files..."); 
     Enter(); 
     Enter(); 

     createDir(source); 

     String forResolve=new String();  
     for(int i=1;i<10;i++){ 
      forResolve=new StringBuilder(String.valueOf(i)).append(".txt").toString(); 
      createFile(source.resolve(forResolve)); 
     } 

     System.out.println("Please enter to create the targetPath and the files..."); 
     Enter(); 

     if(Files.notExists(target)) 
      createDir(target); 
     try(DirectoryStream<Path> contents=Files.newDirectoryStream(source,"*.*")){ 


      System.out.println("Please enter to start copying..."); 
      Enter(); 

      int count=1; 
      for(Path temp:contents){ 

       copyFile(source, target.resolve(temp.getFileName().toString())); 
       count++; 
      } 
      System.out.println("Copy files complete..."); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 


    } 

     static void createFile(Path path){ 
      try{ 
       Files.createFile(path); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 

    static void createDir(Path path){ 
     try{ 
      Files.createDirectories(path); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    static void Enter(){ 
     try{ 
      System.in.read(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    static void copyFile(Path source, Path target){ 
     try{      
       Files.copy(source, target); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

} 

コードの目的は、ソースディレクトリ内のファイルをターゲットディレクトリにコピーすることです。それでも、私がプログラムを実行すると、ターゲットディレクトリにあるものは、ソースディレクトリ内のファイルと同じ名前のフォルダです。言い換えれば、ソースディレクトリにファイル名 "hello.txt"がある場合、プログラムを実行した後、ターゲットディレクトリに "hello.txt"というフォルダ名があります。誰でも上記のコードに何が間違っているか教えていただけますか?前もって感謝します。これは、主な問題のように思える

+3

デバッガでプログラムをステップ実行することをお勧めします。また、ScannerやStringTokenizerではなく普通のBufferedReaderを使用します。 –

+2

投票の終わり:見知らぬ人に検査によるコードの誤りを発見させることは生産的ではありません。デバッガやprintステートメントを使用して問題を特定(または少なくとも分離)してから、さらに具体的な質問に戻ってください(10行[テストケース](http: /sscce.org))。 –

+0

あなたはホイールを発明しました:)。 [commons-io](http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#copyDirectoryToDirectory(java.io.File、java.io.File))を使用します。そこにもソースコードがあります。 –

答えて

0

は、ターゲットパスを決定する際に、IISと、このメソッド

static void createDir(Path path){ 
     try{ 
      Files.createDirectories(path); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 

は間違って動作し、ディレクトリなど全体のパスを作成します。

関連する問題