TuckJohn
TuckJohn

Reputation: 55

Java file moving/copying using the FILE class

I am a relatively novice programmer who can code in HTML, Python, and Java, and I decided to challenged myself to write a block of Java code(using JCreator). I came up with the following challenge:

Design a program in which it would take a .exe file, copy it to a folder (which could be easily changed though a variable), run the file, then delete the file, then continue to do the same process throughout every sub-folder in that directory

I proceeded to write the program, and got the parts of the program to run the exe and to find all of the sub-directories working (with help from stack-overflow, of course). I put all of them together, in addition to the part to copy the .exe file using Files.copy. Every time I run the program, it returns an error.

Part of my code to copy the file, run it, then delete it. I use variables such as "location" and "destination" to allow for easy expansion to the program, and these variables are defined earlier in the program

   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");

    }

Both of these pieces of code are from/used in the same method, so there should not be any problems regarding local variables and whatnot

Any feedback/help would be greatly appreciated!

EDIT: After being run, it returns the "cannot find symbol" error

EDIT #2: I realize that I did not include my imports, which my be the source of the problem My imports:

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;

Upvotes: 1

Views: 805

Answers (1)

Gren
Gren

Reputation: 1854

There were a missing import statement, String arguments instead of Path arguments and a missing 'static call'
I have commented the corrections in the code.

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");

        }
    }
}

And here is an example how to walk through a path:

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;
                }
            });
}

Upvotes: 1

Related Questions