使用FILE类移动/复制Java文件

时间:2014-12-08 16:34:47

标签: java

我是一个相对新手的程序员,可以用HTML,Python和Java编写代码,我决定挑战自己编写一个Java代码块(使用JCreator)。我提出了以下挑战:

设计一个程序,在该程序中,它将获取一个.exe文件,将其复制到一个文件夹(可以通过变量轻松更改),运行该文件,然后删除该文件,然后继续执行相同操作处理该目录中的每个子文件夹

我继续编写程序,并获得程序的各个部分来运行exe并找到所有子目录工作(当然,在堆栈溢出的帮助下)。除了使用Files.copy复制.exe文件的部分之外,我将所有这些放在一起。每次运行程序时,都会返回错误。

我的部分代码复制文件,运行它,然后删除它。我使用" location"等变量。和"目的地"允许轻松扩展到程序,这些变量在程序的前面定义

   public static void EXETestGo(final File folder){

    String destination = folder.getAbsolutePath();  //place to move the exe file; changes each time that the method is called
    String location = "C:/file.exe";                //origin of the exe file
    String runLocation = destination + "/" + "file.exe";    //place of the moved exe file

|

    try{

        Files.copy(location, destination, REPLACE_EXISTING);

        Runtime r = Runtime.getRuntime();       //These two combined
        Process p = r.exec(runLocation);        //'runs' the exe file

        deleteIfExists(runLocation);            //deleates file

    }catch(IOException ex){
        //catches the failed process if it fails

        System.out.println (ex.toString());     //prints out the problem
        System.out.println("Error 404");

    }

这两段代码都来自/使用相同的方法,所以不应该有任何关于局部变量的问题和什么

非常感谢任何反馈/帮助!

编辑:运行后,它会返回"找不到符号"错误

编辑#2:我意识到我没有包含我的导入,这是我的问题的根源 我的进口商品:

import java.io.File;
import static java.nio.file.StandardCopyOption.*;
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;

1 个答案:

答案 0 :(得分:1)

缺少导入语句,String参数而不是Path参数和缺少的静态调用'
我已经评论了代码中的更正。

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.File;
import java.io.IOException;
//Correction: missing import of Files
import java.nio.file.Files;
import java.nio.file.Paths;

public class Test {

    public static void EXETestGo(final File folder){

        String destination = folder.getAbsolutePath();  //place to move the exe file; changes each time that the method is called
        String location = "C:/file.exe";                //origin of the exe file
        String runLocation = destination + "/" + "file.exe";    //place of      


        try{
            //Correction: Files - missing import and the arguments must be of type Path, not String
            Files.copy(Paths.get(location), Paths.get(destination), REPLACE_EXISTING);

            Runtime r = Runtime.getRuntime();       //These two combined
            Process p = r.exec(runLocation);        //'runs' the exe file
            //Correction: 'Files.' missing and argument must be of type Path, not String
            Files.deleteIfExists(Paths.get(runLocation));            //deleates file

        }catch(IOException ex){
            //catches the failed process if it fails

            System.out.println (ex.toString());     //prints out the problem
            System.out.println("Error 404");

        }
    }
}

这是一个如何走过路径的例子:

private static void listDirectoryAndFiles() throws IOException {
    final Path root = Paths.get("C:/Test");
    Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class),
            Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir,
                        BasicFileAttributes attrs) throws IOException {
                    System.out.println(dir.toString());
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {
                    System.out.println(file.toString());
                    return FileVisitResult.CONTINUE;
                }
            });
}