use of org.apache.commons.io.input.ReversedLinesFileReader in project top20 by algorithmica-repository.
the class NthLineFromEnd method findNthLinefromEnd3.
public static String findNthLinefromEnd3(String file, int n) throws Exception {
ReversedLinesFileReader rf = new ReversedLinesFileReader(new File(file));
String line;
int count = 0;
while ((line = rf.readLine()) != null) {
if (++count == n)
return line;
}
rf.close();
return null;
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project top20 by algorithmica-repository.
the class NthLineFromEnd method getNthLineFromEnd3.
public static String getNthLineFromEnd3(String filename, int n) throws Exception {
ReversedLinesFileReader rfr = new ReversedLinesFileReader(new File(filename));
int count = 0;
String line;
while ((line = rfr.readLine()) != null) {
if (++count == n)
return line;
}
return null;
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project top20 by algorithmica-repository.
the class NthLineFromEnd method findNthLinEnd3.
public static String findNthLinEnd3(String file, int k) throws Exception {
ReversedLinesFileReader rfr = new ReversedLinesFileReader(new File(file));
String line = null;
int count = 0;
while ((line = rfr.readLine()) != null) {
if (++count == k)
break;
}
rfr.close();
return line;
}
use of org.apache.commons.io.input.ReversedLinesFileReader in project ats-framework by Axway.
the class LocalFileSystemOperations method getLastLinesFromFile.
@Override
public String[] getLastLinesFromFile(String fileName, int numLinesToRead, Charset charset) {
LinkedList<String> lastLinesList = new LinkedList<String>();
ReversedLinesFileReader reversedReader = null;
try {
reversedReader = new ReversedLinesFileReader(new File(fileName), 4096, charset);
while (lastLinesList.size() < numLinesToRead) {
String line = reversedReader.readLine();
// check if the file has less lines than the wanted
if (line != null) {
lastLinesList.addFirst(line);
} else {
break;
}
}
return lastLinesList.toArray(new String[lastLinesList.size()]);
} catch (IOException ioe) {
throw new FileSystemOperationException("Error reading file '" + fileName + "'", ioe);
} finally {
if (reversedReader != null) {
IoUtils.closeStream(reversedReader);
}
}
}
Aggregations