-3

I have tried many examples from the same question that has already been asked including:

IOUtils.copy();

(copy is a non-existent method)

Files.copy(source, target, REPLACE_EXISTING);

(REPLACE_EXISTING "Cannot find Symbol")

FileUtils.copyFile();

(FileUtils doesn't exist)

The problems with using them are in brackets.

Here is the code for the most repeated method for copying:

import static java.nio.file.Files;
public void Install()
{
    CrtFol();
    CrtImgFol();
    CrtSaveFol();
    CrtSaveFile();
    open.runmm();
    //I have added the import for "Files"
    Files.copy(img1, d4, REPLACE_EXISTING);
    //Compiler says "Cannot find symbol" when I go over REPLACE_EXISTING
    //img1 is a File and d4 is a File as a directory
}

Are there any other ways to copy or a way to fix the one above?

Matthew
  • 185
  • 2
  • 12
  • 2
    Just do static import or explicit reference of [StandardCopyOption](http://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html)? – BalusC Apr 12 '13 at 13:50
  • `FileUtils` is available in `apache commons-io` jar, have you included it? `http://commons.apache.org/proper/commons-io/download_io.cgi` REPLACE_EXISTING should be a boolean value, meaning the value you are passing should be `true` or `false`, you should not copy paste everything as it is – redDevil Apr 12 '13 at 13:51
  • FileUtils and IOUtils are part of [Apache commons-io](http://commons.apache.org/proper/commons-io/) – Bruno Grieder Apr 12 '13 at 13:52
  • BalusC I did include the static import – Matthew Apr 12 '13 at 13:54
  • Can you include your static import in the code in your question given it is critical to understanding what the problem is. – Peter Lawrey Apr 12 '13 at 13:55
  • http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java - have a look at – Mads Apr 12 '13 at 14:00
  • http://stackoverflow.com/q/1146153/62576 or http://stackoverflow.com/q/300559/62576 – Ken White Apr 12 '13 at 14:02
  • @Matthew That static import does not really look like the one BalusC suggested, does it? Try with `import static java.nio.file.StandardCopyOption` instead. – maba Apr 12 '13 at 14:29

4 Answers4

3

With Java 7's standard library, you can use java.nio.file.Files.copy(Path source, Path target, CopyOption... options). No need to add additional dependencies or implement your own.

try {
     Files.copy( Paths.get( sFrom ), 
                 Paths.get( sTo ),
                 StandardCopyOption.REPLACE_EXISTING);
 } catch (IOException e) { 
     // Handle exception
 }
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
1

Not sure if Java actually has anything to copy a file. The simplest way would be to convert the file into a byte stream and then write this stream to another file. Something like this:

        InputStream inStream = null;
        OutputStream outStream = null;

        File inputFile =new File("inputFile.txt");
        File outputFile =new File("outputFile.txt");

        inStream = new FileInputStream(inputFile);
        outStream = new FileOutputStream(outputFile);

        byte[] buffer = new byte[1024];


        int fileLength;
        while ((fileLength = inStream.read(buffer)) > 0){

              outStream.write(buffer, 0, fileLength );

              }

        inStream.close();
        outStream.close();

where inputFile is the file being copied from, and outputFile is the name of the copy.

Ben Green
  • 3,953
  • 3
  • 29
  • 49
  • I believe so. I have googled it and found an example with jpg, so should do. Let me know the outcome if you use this option :) – Ben Green Apr 12 '13 at 14:08
  • I tried it but for some reason now the JVM can't find the file. – Matthew Apr 12 '13 at 14:32
  • Are you giving the full path? – Ben Green Apr 12 '13 at 14:34
  • The picture is in a folder that is in the code, and is to be copied to the Roaming folder. I tried "img\\P.png". Img is the name of the folder yet it can't find it. java.io.FileNotFoundException: img\P.png (The system cannot find the path specified) – Matthew Apr 12 '13 at 14:43
  • If you are working in the src directory, then doing that will mean that it is looking for the img folder inside the src folder. Try instead working out where it is relatively from where you are writing your code, or alternatively give the absolute path. – Ben Green Apr 12 '13 at 15:01
  • Although I'm sure i got it right because I used the directory on a JLabel. – Matthew Apr 12 '13 at 15:09
1

I use this code:

import java.io.*;

public class CopyTest {

    public CopyTest() {

    }

    public static void main(String[] args) {
        try {
            File stockInputFile = new File("C://test.txt");
            File StockOutputFile = new File("C://output.txt");

            FileInputStream fis = new FileInputStream(stockInputFile);
            FileOutputStream fos = new FileOutputStream(StockOutputFile);
            int count = 0;

            while((count = fis.read()) > -1){
                fos.write(count);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            System.err.println("FileStreamsReadnWrite: " + e);
        } catch (IOException e) {
            System.err.println("FileStreamsReadnWrite: " + e);
        }

   }

}
Supremenerd88
  • 47
  • 1
  • 7
0

Use this code to upload file, I am working on SpringBoot...

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;


import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;


@Component
public class FileUploadhelper {
    public final String uploadDirectory = "D:\\SpringBoot Project\\BootRestBooks\\src\\main\\resources\\static\\image";

    public boolean uploadFile(MultipartFile mf) {

        boolean flag = false;


        try {

            Files.copy(mf.getInputStream(), Paths.get(uploadDirectory + "\\" + mf.getOriginalFilename()), StandardCopyOption.REPLACE_EXISTING);
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }
}