Sto cercando un modo per leggere i file di testo grandi.Ho trovato un codice in rete che per me è il migliore : http://andreafrancia.it/articoli/large-file-viewer/
Ci sta un problema però....nella stama mi salta sempre la prima riga come mai?
Posto il codice
codice:public class LazyFileListModel extends AbstractListModel { private static Logger logger = Logger.getLogger(LazyFileListModel.class.getName()); private RandomAccessFile lineCounterFile; private List<Long> linePositions = Collections.synchronizedList(new ArrayList<Long>()); private RandomAccessFile readerFile; public LazyFileListModel(File file) throws FileNotFoundException { this.lineCounterFile = new RandomAccessFile(file, "r"); this.readerFile = new RandomAccessFile(file, "r"); Runnable lineCounterThread = new Runnable() { public void run() { try { String line; while ((line = lineCounterFile.readLine()) != null) { linePositions.add(lineCounterFile.getFilePointer()); Thread.yield(); } linePositions.remove(linePositions.size() - 1); } catch (IOException ex) { logger.log(Level.SEVERE, "", ex); } } }; Executors.newSingleThreadExecutor().submit(lineCounterThread); Runnable firer = new Runnable() { public void run() { int last = -1; int current = linePositions.size() - 1; if (last < current) { fireIntervalAdded(LazyFileListModel.this, last + 1, current); last = current; } } }; Executors.newScheduledThreadPool(1).scheduleAtFixedRate(firer, 2, 500, TimeUnit.MILLISECONDS); } public int getSize() { return linePositions.size(); } public Object getElementAt(int index) { try { readerFile.seek(linePositions.get(index)); return String.format("%06d: %s", index, readerFile.readLine()); } catch (IOException ex) { logger.log(Level.SEVERE, "", ex); return null; } } }

Rispondi quotando

