using cache'd data if dir contents changed

J

jimgardener

hi
i wrote some code that checks if any file is added or removed from a
folder.
initially it creates an ArrayList of files with a specific extension
(say .png) in a directory .Then it checks the cachefile.The cachefile
is an Object[] with 2 elements,ie,the arraylist and the data (here i
am using a double[][])
Then i check if the arraylist created now matches with the arraylist
from cachefile.if it does, then i infer that the folder contents are
not changed.(I do not take into account renaming of files in the
directory.Only addition and removal of files are considered )
if contents of folder are not changed ,the code extracts 'data' from
the cache file.

if there is no cachefile or if the arraylist created now doesn't match
the one from cache,then i create the data(double[][]) and put the
arraylist and the double[][] into cells of an Object[] and write them
to cachefile

here is the code.I would like to know if this can be done in a more
efficient way.Also,i am not sure how to handle the unckecked cast
warning i get when i compile it using javac -Xlint:unchecked
CacheDemo.java.Any help /advice greatly appreciated
thanks
jim


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
class CacheDemo{
public void checkCache(String dir,String extension){
final String ext="."+extension;
String[] children=null;

double[][] data=null;
File dirf=new File(dir);
//if a directory get the contents
if(dirf.isDirectory()){
children=dirf.list(new FilenameFilter(){
public boolean accept(File f,String name){
if(name.endsWith(ext))
return true;
else
return false;
}
});
}
else{
System.out.println("not a directory");
System.exit(1);
}
//make a list of the contents
List<String> imglist=new ArrayList<String>();
for (String i : children){
String filename=dir+File.separator+i;
imglist.add(filename);
}
Collections.sort(imglist);

/*
check cachefile which contains an array of 2 objects ,one a List
object containig names of folder contents
and the next one a double[][] that is our data
*/
try{
FileInputStream fin=new FileInputStream("mycache.cache");
ObjectInputStream fo = new ObjectInputStream(fin);
Object[] cacheobjs=(Object[])fo.readObject();
fo.close();
fin.close();

//first element is a List<String>
List<String> files=(List<String>)cacheobjs[0];

/* if contents of the List same as those of imglist
then folder contents unchanged
*/
if(files.equals(imglist)){
System.out.println("folder contents unchanged,can use cache's
data");
//get the second element of Object[]
data=(double[][])cacheobjs[1];
System.out.println("the recovered data is=");
//show the data contents
for(int i=0;i<data.length;i++){
for(int j=0;j<data[0].length;j++){
System.out.print(data[j]+",");
}
System.out.println("");
}
}
else{

System.out.println("dir contents changed..need to create a new
cache");
writeCache(imglist);
}


}
catch(FileNotFoundException e){
System.out.println("no such file.need to create a cache and store
it");

writeCache(imglist);
e.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
catch(ClassNotFoundException cne){
cne.printStackTrace();
}
}
private double[][] createNewData(){
double[][] newdata=new double[][]{
{1.,3.,5.,7.},
{2.,4.,6.,8.},
{2.,5.,6.,7.}
};
return newdata;
}
private void writeCache(List<String> filenames){
Object[] cachearray=new Object[2];
cachearray[0]=filenames;
//create some data and store
double[][] newdata=createNewData();
System.out.println("created new data");
cachearray[1]=newdata;

try{
FileOutputStream fout = new FileOutputStream("mycache.cache");
ObjectOutputStream fos = new ObjectOutputStream(fout);
fos.writeObject(cachearray);
System.out.println("wrote cache");
fos.close();
fout.close();

}
catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args){
String dir="D:\\cachetest";
String ext="png";
new CacheDemo().checkCache(dir,ext);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top