Forse il titolo rende poco l'idea della mia domanda, ma proverò a spiegarmi meglio qui. Io ho una classe che carica un file, questa per intenderci:
codice:
public abstract class FileHandler { private String filePath;
private ArrayList<HTTPFormat> parsed = new ArrayList<>();
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public ArrayList<HTTPFormat> getParsed() {
return parsed;
}
public void setParsed(ArrayList<HTTPFormat> parsed) {
this.parsed = parsed;
}
public ArrayList<String> load(){
ArrayList<String> letto = new ArrayList<>();
try {
String thisLine = null;
File f = new File(filePath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while((thisLine = br.readLine())!=null){
letto.add(thisLine);
}
br.close();
} catch (IOException ex) {
System.err.println("Errore nella lettura del file");
}
return letto;
}
public abstract void parse();
public void write(String s, String destPath){
try {
File out = new File(destPath);
FileWriter fw = new FileWriter(out);
fw.write(s);
fw.close();
} catch (IOException ex) {
System.err.println("Errore nella scrittura del file");
}
}
}
In parole povere bisogna settare un percorso per il file (filePath) per ottenere il file già parsato (trattasi di file di log HTTP) nella lista parsed. Ora, siccome il caricamento del file è un'attività dispendiosa di tempo, vorrei mettere il metodo load() nel costruttore della classe, per avere una lista delle righe del file già in memoria. Una volta che metto il metodo load() nel costruttore e vado di test ho un errore, poichè non ha il filePath, che però io gli setto in questa classe di test.
codice:
public class LoadTest { public static void main(String[] args) {
FileHandler fh = new FileHandler() {
@Override
public void parse() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
fh.setFilePath(Constants.LOG_HTTP_PATH);
for (Iterator<String> i = fh.load().iterator(); i.hasNext();) {
String next = i.next();
System.out.println(next);
}
}
}
Qualcuno mi può suggerire un'idea per risolvere il problema? Sono disponibile per chiarimenti