Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java命令行参数将多个文件移动到PC中的单个位置_Java_File - Fatal编程技术网

使用java命令行参数将多个文件移动到PC中的单个位置

使用java命令行参数将多个文件移动到PC中的单个位置,java,file,Java,File,我正在尝试编写一个程序,使用多个线程将多个文件复制到电脑中的一个位置 我的输入将是文件的路径(在我的电脑中 0作为命令行参数。 我必须同时将多个文件复制到电脑中的特定文件夹 文件源是命令行输入 文件目标应该是我电脑中的新文件夹。 需要知道如何将目标路径指定为目录,以及如何将输入作为命令行参数 这就是我正在调用的方法 private static void copyFileUsingChannel(File source, File dest) throws IOException {

我正在尝试编写一个程序,使用多个线程将多个文件复制到电脑中的一个位置

我的输入将是文件的路径(在我的电脑中 0作为命令行参数。 我必须同时将多个文件复制到电脑中的特定文件夹

文件源是命令行输入 文件目标应该是我电脑中的新文件夹。 需要知道如何将目标路径指定为目录,以及如何将输入作为命令行参数 这就是我正在调用的方法

private static void copyFileUsingChannel(File source, File dest) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel destChannel = null;
        try {
            sourceChannel = new FileInputStream(source).getChannel();
            destChannel = new FileOutputStream(dest).getChannel();
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
           }finally{
               sourceChannel.close();
               destChannel.close();
           }

如果要使用命令行,只需检查main方法中的args[]数组

public static void main( String args[] ){
    if( args.length >= 2 ){
       File source = new File(args[0]);
       File dest = new File( args[1] );

       // Check files
       if( source.exists() && source.isDirectory() ){
          // continue check here
       }else{
         // args[0] is not a directory or does not exists
       }
    }
}
如果需要迭代文件,只需使用

File[] files = source.listFiles();
for( File file : files ){
   if( file.isDirectory() ){
     // do stuff
   }
}

您可以将每个
文件
添加到
ExecutorService
中,并允许它在线程可用时“弹出”下一个作业…哦,您的问题是?您可能还想看看我的源代码是一个路径,所以args[0]只接受第一个字符串,所以我该怎么做,目标应该是我的PC中的一个空文件夹让我们假设您像java-jar myprogram c:/path1 c:/empty那样执行您的程序,那么您可以查看上面的代码如果您只有一个命令参数,您必须在程序中指定空文件夹应该是什么。这可以在.properties中完成您在程序外部指定的文件,并使用Spring、Guice或普通属性加载器加载设置。我将使用4个命令提示窗口,将4个文件上载到另一个驱动器上的同一文件夹中,我希望移动后文件名保持不变。我使用了四个线程和方法调用来复制该文件