Niezken Wahyu Divilo Niezken Wahyu Divilo -4 years ago 136
Java Question

Java: Read all .txt file from folder then move to new folder

Help, i need to read every .txt file in a folder then copy/move it all to the new folder.
But, i need to copy the text inside every .txt file and create them a variable (1 .txt file to 1 variable). I only got this code that only can read 1 .txt file then copying the text inside the .txt file to another .txt file in another folder and it creating the text inside .txt into variable (aLine)...

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

String source = File.separator + "C:\\Users\\NN\\Documents\\Test1\\Source.txt";
String dest = File.separator + "C:\\Users\\NN\\Documents\\Test2\\Empty.txt";

File dir = new File(".");

File fin = new File(source);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));

FileWriter fstream = new FileWriter(dest, true);
BufferedWriter out = new BufferedWriter(fstream);

String aLine = null;
while ((aLine = in.readLine()) != null) {



Answer Source

You have 4 little problems here:

  1. Get the files from a directory.
  2. Filter them by their filename suffix.
  3. Get their content and save them somewhere.
  4. Move the files to another directory.

If you organize your code like that, it gets quite easy (Java 8):

import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        String source = "source";
        String target = "target";

        List<Path> filePaths = filePathsList(source); // Step 1: get all files from a directory
        List<Path> filteredFilePaths = filter(filePaths); // Step 2: filter by ".txt"
        List<String> contentOfFiles = getContentOfFiles(filteredFilePaths); // Step 3: get content of files
        move(filteredFilePaths, target); // Step 4: move files to destination

    public static List<Path> filePathsList(String directory) throws IOException {
        List<Path> filePaths = new ArrayList<>();
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(FileSystems.getDefault().getPath(directory));
        for (Path path : directoryStream) {
        return filePaths;

    private static List<Path> filter(List<Path> filePaths) {
        List<Path> filteredFilePaths = new ArrayList<>();
        for (Path filePath : filePaths) {
            if (filePath.getFileName().toString().endsWith(".txt")) {
        return filteredFilePaths;

    private static List<String> getContentOfFiles(List<Path> filePaths) throws IOException {
        List<String> contentOfFiles = new ArrayList<>(filePaths.size());
        for (Path filePath : filePaths) {
            String content = new String(Files.readAllBytes(filePath));
        return contentOfFiles;

    private static void move(List<Path> filePaths, String target) throws IOException {
        Path targetDir = FileSystems.getDefault().getPath(target);
        if (!Files.isDirectory(targetDir)) {
            targetDir = Files.createDirectories(Paths.get(target));
        for (Path filePath : filePaths) {
            System.out.println("Moving " + filePath.getFileName() + " to " + targetDir.toAbsolutePath());
            Files.move(filePath, Paths.get(target, filePath.getFileName().toString()), StandardCopyOption.ATOMIC_MOVE);

    private static void printToConsole(List<String> contentOfFiles) {
        System.out.println("Content of files:");

With package java.nio it's just that easy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download