Skip to Main Content

How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap?

2801625May 1 2015 — edited May 4 2015

How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap? Currently my program reads the data from several text files in a directory and the saves that information in a text file called output.txt. I would like to cache this data in order to use it later. How can I do this using the TreeMap Class? These are the keys,values: TreeMap The data I'd like to Cache is (date from the file, time of the file, current time).

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

  // open the Printwriter before your loop

  PrintWriter outputStream = new PrintWriter(output);

  for (File textfiles : files) {

  if (textfiles.isFile() && textfiles.getName().endsWith(".txt")) {

  BufferedReader inputStream = null;

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

  }

  }

  }

  }

  }

}

Comments
Post Details
Added on May 1 2015
4 comments
927 views