import java.io.*; import java.util.*; import java.nio.channels.FileChannel; /** * * * @author Sacchi Matteo, Sassi Federico * @version 0.1 */ public class TargheReaderRed implements DataReaderTarghe { private File file; private ArrayList input; private Map in_out; private Map occurencies; public TargheReaderRed(File file){ this.file = file; in_out = new HashMap(); occurencies = new HashMap(); input = new ArrayList(); File temp = null; if(file.isDirectory()) { java.io.FilenameFilter select = new FileListFilter(null, "txt"); File sources[] = file.listFiles(select); FileOutputStream outf = null; FileInputStream inpf = null; try { temp = File.createTempFile("Classifier_save", null, new File("./ts/")); outf = new FileOutputStream(temp, true); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel out = outf.getChannel(); for(int i = 0; i < sources.length; i++) { try { inpf = new FileInputStream(sources[i]); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } FileChannel in = inpf.getChannel(); try { long bytesWritten = 0L; long bytesCount = in.size(); while (bytesWritten < bytesCount) { bytesWritten += in.transferTo(bytesWritten, bytesCount - bytesWritten, out); } inpf.close(); } catch(IOException e) { e.printStackTrace(System.err); System.exit(1); } } file = temp; } else if(!file.isFile()) { System.err.println(file + " : nome file non esistente..."); System.exit(1); } else { String name = file.getName(); if(!name.endsWith(".txt")) { System.err.println(file.getName() + " : formato file non valido..."); System.exit(1); } } } public void Load(){ try{ FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String s,sin,sout,opz,res; while ((s = br.readLine()) != null) { s = s.trim(); if (s.indexOf("/")!=-1 || s.indexOf("#")!=-1 ){ continue; } s = s.replaceAll("[^0-9]", "").replaceAll("/s",""); sin = s.substring(0,28); sout = s.substring(28); LongBox in = new LongBox(Long.parseLong(sin,2)); LongBox out = new LongBox(Long.parseLong(sout)); input.add(in); in_out.put(in,out); Long tmp = (Long) occurencies.get(new Long(out.long_value)); if (tmp == null) occurencies.put(new Long(out.long_value),new Long(1)); else { tmp = new Long((tmp).longValue()+1); occurencies.put(new Long(out.long_value), tmp); } } } catch (FileNotFoundException e){ e.printStackTrace(System.err); System.exit(-1); } catch (IOException e){ e.printStackTrace(System.err); System.exit(-1); } } public long getOccurencies(Long key){ return ((Long)occurencies.get(key)).longValue(); } public Map getMap(){ return in_out; } public ArrayList getInputObjects(){ return input; } public boolean isExt(){ return false; } }