0

In my program I'm trying to print my output to a file but the file is empty. The program reads text files in a directory. I get the desired output to the console but not in the output.text file. Not sure what is missing in the program. thanks in advance for the help.

import java.io.*;

public class CacheData {

    public static void main(String[] args) throws IOException {
        String target_dir = "C:\\Files";
        String output = "C:\\Files\\output.txt";
        File dir = new File(target_dir);
        File[] files = dir.listFiles();

        for (File textfiles : files) {
            if (textfiles.isFile() && textfiles.getName().endsWith(".txt")) {
                BufferedReader inputStream = null;

                try {
                    inputStream = new BufferedReader(new FileReader(textfiles));
                    String line;

                    while ((line = inputStream.readLine()) != null) {
                        System.out.println(line);


                    PrintWriter outputStream = new PrintWriter(output);
                    outputStream.println();
                    outputStream.close();
                    }

                } finally {
                    if (inputStream != null) {
                        inputStream.close();

                    }
                }
            }
        }
    }

}
FullNelson
  • 99
  • 2
  • 13
  • 1
    Are you intending to print what is in `line`? If so, pass `line` to `outputStream.println()` – Robb Apr 30 '15 at 19:10

3 Answers3

1

Looks like you're wanting to combine the contents of multiple files (text files in your case) into a single file.

Here's an IMO simpler solution. Simpler in that you don't have to worry about safely opening and closing the files yourself as you read or write.

public static void main(String[] args) throws Exception {
    String targetDir = "Path to your directory of input files";
    String outputFile = "Path to your output file";

    File dir = new File(targetDir);
    File[] files = dir.listFiles(new FilenameFilter() {
        // Only get files that end with .txt
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".txt");
        }
    });

    // Read all file lines into a List
    List<String> inputFileLines = new ArrayList<>();
    for (File file : files) {
        inputFileLines.addAll(Files.readAllLines(Paths.get(file.getAbsolutePath())));
    }

    // Write the List to the console
    for (String line : inputFileLines) {
        System.out.println(line);
    }

    // Write the List to a single file
    Files.write(Paths.get(outputFile), inputFileLines, StandardOpenOption.CREATE);
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0
                    PrintWriter outputStream = new PrintWriter(output);

                    //you are not printing anything to output file.
                    outputStream.println();

                    //Change it to
                    outputStream.println(line);
                    outputStream.close();
K139
  • 3,654
  • 13
  • 17
  • When I made the change using this suggestion the output file only contained 1 word. The text files I'm reading contain thousands of words. I'm able to generate the output to the console but its not writing to the file. – FullNelson May 01 '15 at 12:34
0

Some modifications to your code to write to your file. Please see the inline-comments for further informations.

import java.io.*;

public class CacheData {
public static void main(String[] args) throws IOException {
    String target_dir = "C:\\Files";
    String output = "C:\\Files\\output.txt";
    File dir = new File(target_dir);
    File[] files = dir.listFiles();

    for (File textfiles : files) {
        if (textfiles.isFile() && textfiles.getName().endsWith(".txt")) {
            BufferedReader inputStream = null;

            try {
                inputStream = new BufferedReader(new FileReader(textfiles));
                String line;
                //open the Printwriter before your loop
                PrintWriter outputStream = new PrintWriter(output);
                while ((line = inputStream.readLine()) != null) {
                    System.out.println(line);


                //Write Content not just linebreaks
                outputStream.println(line);

                }
                //close the outputstream after the loop
                outputStream.close();
            } finally {
                if (inputStream != null) {
                    inputStream.close();

                }
            }
        }
    }
}

}

To write the content of every txt-File in your file, move the Printwriter before the for-loop and close the printwriter after the for-loop and not just the while-loop.

daniel
  • 3,166
  • 2
  • 17
  • 18