Search in sources :

Example 6 with ReversedLinesFileReader

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;
}
Also used : ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) File(java.io.File)

Example 7 with ReversedLinesFileReader

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;
}
Also used : ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) File(java.io.File)

Example 8 with ReversedLinesFileReader

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;
}
Also used : ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 9 with ReversedLinesFileReader

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);
        }
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ReversedLinesFileReader(org.apache.commons.io.input.ReversedLinesFileReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) LinkedList(java.util.LinkedList)

Aggregations

ReversedLinesFileReader (org.apache.commons.io.input.ReversedLinesFileReader)9 File (java.io.File)6 IOException (java.io.IOException)3 RandomAccessFile (java.io.RandomAccessFile)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1