Lunar
Lunar

Reputation: 509

Java Moving Files

Right now, I'm trying to move files and folders from a directory onto my Desktop. Currently here's my code:

import java.nio.file.*;
import java.nio.FileUtils;

class CopyDirectoryApache {

public static void main(String[] args) throws IOException {

    File sourceLocation = new File("/home/jakobcurrin/minecraft/mods/");
    File targetLocation = new File("/home/jakobcurrin/desktop/");

    FileUtils.copyDirectory(sourceLocation, targetLocation);
  }
}

Main.java:6: error: class CopyDirectory is public, should be declared in a 
file named CopyDirectory.java
public class CopyDirectory
       ^
Main.java:3: error: cannot find symbol
import java.nio.FileUtils;
               ^
  symbol:   class FileUtils
  location: package java.nio
2 errors

I debugged the code, and it keeps saying "Cannot find symbol." How can I fix this? If you could explain thoroughly, that would be wonderful.

Upvotes: 3

Views: 660

Answers (2)

Alvaro Gili
Alvaro Gili

Reputation: 23

You have several errors.

The first: Main.java:6: error: class CopyDirectory is public, should be declared in a file named CopyDirectory.java is because the file's name is different to the class' name.

And the second is because you don't have the dependency or library java.nio

Upvotes: 0

Michael Kemmerzell
Michael Kemmerzell

Reputation: 5276

import java.io.*

This should fix your problem. Just remove the 'n' and use a different library. The class File (Documentation) has some pretty good methods for file operations.

isDirectory() lets you check if your path is correct.

On the other hand you could use this method:

import static java.nio.file.StandardCopyOption.*; 
Files.copy(source, target, REPLACE_EXISTING);

The method copies all files from source to target.

Upvotes: 1

Related Questions