|
Author |
Topic: loadStrings(fileName,startLine) (Read 336 times) |
|
benelek
|
loadStrings(fileName,startLine)
« on: Nov 11th, 2003, 2:13pm » |
|
in reading really large (and growing) csv or txt files, it'd be great to be able to only look at part of the file (such as the last 10 lines). right now we have: loadStrings(fileName); a possibility: loadStrings(fileName, startIndex); this would be really useful in situations where a file is either really large or is being continually added to by some other program (such as a log file). speed increases would be felt when having to load a file once per loop (in order to get the most up-to-date version of the file).
|
|
|
|
fry
|
Re: loadStrings(fileName,startLine)
« Reply #1 on: Nov 11th, 2003, 6:03pm » |
|
hm.. not too likely with loadStrings() since it's just intended to be a replacement for that ten lines of code you write to slurp in a single file. i plan to write some other classes to handle i/o on large files/csv files, log files, that sort of thing for my thesis work; so that's one way this will happen in the future, but for the time being, here's the source for loadStrings(), which you're welcome to make your own version for your apps: Code: public String[] loadStrings(String filename) { try { return loadStrings(loadStream(filename)); } catch (IOException e) { System.err.println("problem loading strings from " + filename); e.printStackTrace(); } return null; } public String[] loadStrings(InputStream input) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String lines[] = new String[100]; int lineCount = 0; String line = null; while ((line = reader.readLine()) != null) { if (lineCount == lines.length) { String temp[] = new String[lineCount << 1]; System.arraycopy(lines, 0, temp, 0, lineCount); lines = temp; } lines[lineCount++] = line; } reader.close(); if (lineCount == lines.length) { return lines; } // resize array to appropraite amount for these lines String output[] = new String[lineCount]; System.arraycopy(lines, 0, output, 0, lineCount); return output; } catch (IOException e) { e.printStackTrace(); } return null; } |
|
|
|
|
|
fry
|
Re: loadStrings(fileName,startLine)
« Reply #2 on: Nov 11th, 2003, 6:04pm » |
|
durn those indents. the code doesn't *really* look that shitty.. seems that somewhere in the copy/paste or how this board handles code blocks we've lost a bunch of spaces..
|
|
|
|
benelek
|
Re: loadStrings(fileName,startLine)
« Reply #3 on: Nov 12th, 2003, 5:09am » |
|
thanks Fry, that code'll speed things along. the spacing quirks of this board occur even when you edit the code within the browser. it's strange, but it makes you appreciate "beautify" even more
|
|
|
|
madmerv Guest
|
Re: loadStrings(fileName,startLine)
« Reply #4 on: Nov 14th, 2003, 11:02pm » |
|
for information on ways to handle this, consult Perl. The addition of a working perl interpreter would be interesting... ways to search strings such as: string PLQuery( String instr, String pl) { static String outstr; /* Interpret Perl "pl" on data "instr" */ return outstr; }
|
|
|
|
|