Search code examples
javacachingtreemap

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


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).

Here is an example of the data contained in the .text. files.

and are as at award awards be but by centsales for he hello her hers his if in into is it me mine no not of on or s such t that the their them then there these they this to was will with you your yours 他 他们 你 你们 我 我们 a an and are as at be but by for if in into is it no not

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

                    }
                }
            }
        }
    }

}

Solution

  • Cache means to have it memory, you are already putting each line in memory line = inputStream.readLine() and then discarding that in next loop iteration.

    you have mentioned you want to store it in TreeMap, you need to decide what will be the key?, as the TreeMap is sorted, how do you want to sort it ?

    import java.io.*;
    import java.util.Map;
    import java.util.TreeMap;
    
    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();
    
            if (files == null || files.length < 1) {
                System.out.println("File list is empty...");
                return;
            }
    
            // open the Printwriter before your loop
            PrintWriter outputStream = new PrintWriter(output);
            //( //comparator if you want something else than natural ordering)
            Map<String, DataContent> myCachedTreeMap = new TreeMap<String, DataContent>();
    
            for (File textFile : files) {
                if (textFile.isFile() && textFile.getName().endsWith(".txt")) {
                    BufferedReader inputStream = null;
                    // close the outputstream after the loop
                    outputStream.close();
                    String content = "";
                    try {
                        inputStream = new BufferedReader(new FileReader(textFile));
                        String line;
                        while ((line = inputStream.readLine()) != null) {
                            content += line;
                            System.out.println(line);
                            // Write Content
                            outputStream.println(line);
                        }
                        //create content
                        DataContent dataContent = new DataContent(System.currentTimeMillis(), textFile.lastModified(), content, textFile.getName());
                        //add to your map
                        myCachedTreeMap.put(textFile.getName(),dataContent );
    
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
    
                        }
                    }
                }
            }
    
            String fileNameYouWantFromCache = "myFile.txt";
            //how to use it.
            DataContent dataContent = myCachedTreeMap.get(fileNameYouWantFromCache);
            System.out.println(fileNameYouWantFromCache +" : \n"+ dataContent);
        }
    
    
        public static class DataContent {
            private long cachedTime; //currentTime
            private long lastModifiedTimestamp;
            private String contents;
            private String fileName; //not sure if you want it
    
            public DataContent(long cachedTime, long lastModifiedTimestamp, String contents, String fileName) {
                this.cachedTime = cachedTime;
                this.lastModifiedTimestamp = lastModifiedTimestamp;
                this.contents = contents;
                this.fileName = fileName;
            }
    
            public long getCachedTime() {
                return cachedTime;
            }
    
            public long getLastModifiedTimestamp() {
                return lastModifiedTimestamp;
            }
    
            public String getContents() {
                return contents;
            }
    
            public String getFileName() {
                return fileName;
            }
    
            @Override
            public String toString() {
                return "DataContent{" +
                        "fileName='" + fileName + '\'' +
                        ", contents='" + contents + '\'' +
                        ", lastModifiedTimestamp=" + lastModifiedTimestamp +
                        ", cachedTime=" + cachedTime +
                        '}';
            }
        }
    }
    

    Note that you will have to define "myKey" -- that is how you are going to lookup your treemap.. you should decide how you want to store value (here we are storing the line/string you read from file as your map value)