Hi,
I want to read the first 3 or 4 lines of a 15MB TXT file.
Can I do this without loading the whole file into memory/variable?
function readFile(_oFile) {
//
// Use BufferedReader so we don't have to read the whole file into memory
//
var _oFR = new Packages.java.io.FileInputStream(_oFile),
_oIR = new Packages.java.io.InputStreamReader(_oFR, "UTF8"),
_oBR = new Packages.java.io.BufferedReader(_oIR),
_sLine = "dummy",
_nReadLine = 0;
// using a database transaction (might/will) speed things up
databaseManager.startTransaction();
try {
while (_sLine) {
_sLine = _oBR.readLine();
_nReadLine++;
if (_sLine) {
// Put your processing code here
}
}
// Save any unsaved data
databaseManager.saveData();
//
//do NOT forget this close! to prevent memory leaks
//
_oBR.close();
// Close the database transaction
databaseManager.commitTransaction();
} catch (_oErr) {
_oBR.close();
application.output("ERROR: " + _oFile.getName() + " at row " + _nReadLine, LOGGINGLEVEL.ERROR);
application.output("ERROR: " + _oErr, LOGGINGLEVEL.ERROR);
databaseManager.rollbackTransaction();
return; // stop process
}
//
// garbage collection
//
_oFR = null;
_oIR = null;
_oBR = null;
}
ROCLASI wrote:I recently found out that this doesn't read files in with the proper encoding (in my case UTF-8).
The following code does. Hope this helps.
Users browsing this forum: No registered users and 2 guests