settingsAccountsettings
Menusettings

Q: Copy a text file from one place to another

+14 votes

How can I copy a text file from one place to another in Java? For example I want a text to be copied to another text file, but with different name (maybe even in the same directory). How can I do it? What kind of Classes and methods can I use? thanks

asked in Java category by user ak47seo
edited by user golearnweb

3 Answers

+3 votes

You can manage files with Java 7's new IO library: with the Class Path

In my code I have a directory (called files - IT MUST BE IN YOUR PROJECT ROOT) with text file in it called: textForCopy.txt

copying text files in java

Then I use:

  • Class Path
  • Class Paths + it's method .get()
  • Class Files + it's method .copy()
  • You can see the arguments in the methods in the code

The code:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class CopyFilesMethod1 {

    public static void main(String[] args) {
        Path source = Paths.get("files", "textForCopy.txt");
        Path targetFile = Paths.get("files", "target.txt");

        try {
            Files.copy(source, targetFile, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("File copied! :-)");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
answered by user eiorgert
edited by user golearnweb
+2 votes

There is another way of copying text files in Java  - the more common one - since it is the older way of doing it - it is available with all versions of Java (not only with Java 7+);

I mean - managing files with the original File class.

I use the 3 Classes:

  1. FileReader
  2. BufferedReader + it's method .readline() (in the infinite while loop)
  3. FileWriter

Here's my code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

public class CopyFilesMethod1Older {

    public static void main(String[] args) {
        String TextFileOriginal = "files/TextFileOriginal.txt";
        String TextFileToBeCopied = "files/TextFileCopied.txt";
        try (
                FileReader fReader = new FileReader(TextFileOriginal);
                BufferedReader bReader = new BufferedReader(fReader);
                FileWriter writer = new FileWriter(TextFileToBeCopied);
        ) {
            while (true) {//INFINITE LOOP
                String line = bReader.readLine();
                if (line == null) {
                    break;
                } else {
                    writer.write(line + "\n");
                }
            }
            System.out.println("File copied!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

answered by user hues
+1 vote

You can use Apache Common libraries: http://commons.apache.org/ and it's utility classes :-)

Here's how to do it:

1. Go to: http://commons.apache.org/proper/commons-io/download_io.cgi

2. Download the library: commons-io-2.5-bin.zip (SEE THE IMAGE BELOW)

apache commons library commons io 2.5 bin

3. UNZip it

4. Use ONLY THE FILE commons-io-2.5.jar

5. Copy the library commons-io-2.5.jar and put it into your project by creating new directory named lib and pasting it there

6. Add the library to the project by clicking on it with the right button and choose Add as Library (now everything in this .jar file is available to your project)

7. See my code

The Code:

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class CopyFilesMethod3ApacheCommons {

    public static void main(String[] args) {

        File sourceFile = new File("files/loremipsum.txt");
        File targetFile = new File("files/target.txt");

        try {
            FileUtils.copyFile(sourceFile, targetFile);
            System.out.println("File is copied!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

BTW you can use it with your Android projects.

answered by user matthew44
edited by user golearnweb
...