下面的Java代码有什么问题?

时间:2012-03-28 07:00:18

标签: java java-7

以下是代码。

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”。任何人都可以告诉我上面的代码有什么问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

似乎主要问题是确定目标路径然后是这个方法

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

工作错误,并将整个路径创建为目录。