Skip to Main Content

My output file is blank.

2801625May 4 2015 — edited May 4 2015

In the program below I'm trying to read the contents of several .txt files in the same diretory and create a new .txt file containing all of the data from each file.

My output is generated in the console however my .text file is blank.

 

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();

// open the Printwriter before your loop

  PrintWriter outputStream = new PrintWriter(output);

// close the outputstream after the loop

// outputStream.close();

  try {

  inputStream = new BufferedReader(new FileReader(textfiles));

  String line;

  while ((line = inputStream.readLine()) != null) {

  System.out.println(line);

  // Write Content

  outputStream.println(line);

  }

  } finally {

  if (inputStream != null) {

  inputStream.close();

  }

  }

  }

  if (outputStream != null) {

  outputStream.close();

  }

  }

  }

}

Message was edited by: 2801625 I made changes to flush and close the output stream.

This post has been answered by aJohny on May 4 2015
Jump to Answer
Comments
Post Details
Added on May 4 2015
13 comments
1,957 views