如何使用TreeMap将正在读取的文本文件集合中的数据缓存在目录中?当前,我的程序从目录中的多个文本文件中读取数据,并将该信息保存在名为output.txt的文本文件中。我想缓存此数据以便以后使用。如何使用TreeMap类做到这一点?这些是键,值:TreeMap我想要缓存的数据是(来自文件的日期,文件的时间,当前时间)。
这是.text中包含的数据的示例。文件。
并按奖励出售,因为他向她打招呼,她是她的,如果我不是我的,那么,他们他们那里然后这些他们将与您同在吗?一个和一个在原处,但是如果被插入,那不是
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();
}
}
}
}
}
}
缓存意味着拥有它的内存,您已经将每一行放入内存中line = inputStream.readLine()
,然后在下一个循环迭代中将其丢弃。
您已经提到过要将其存储在TreeMap中,您需要确定键是什么?在对TreeMap进行排序时,您想如何对其进行排序?
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 +
'}';
}
}
}
请注意,您将必须定义“ myKey” -这就是您要查找树形图的方式。.您应决定如何存储值(此处,我们将从文件中读取的行/字符串存储为地图值)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句