Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.io.IOException;

import java.util.LinkedList;
import android.util.Log;

public class Main {
    private static String TAG = "DEFAC[Helpers]";

    /**
     * 
     * @param aFile
     * @return
     */
    public static String getLastLines(String filename, int number) {
        File aFile = new File(filename);
        StringBuilder contents = new StringBuilder();
        LinkedList<String> ll = new LinkedList<String>();

        try {
            BufferedReader input = new BufferedReader(new FileReader(aFile));
            try {
                String line = null;
                while ((line = input.readLine()) != null) {
                    ll.add(line);
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
        }

        if ((ll.size() - number) <= 0) {
            Log.e(TAG, "Requested number of lines exceeds lines of file");
            return "Requested number of lines exceeds lines of file";
        }

        for (int i = (ll.size() - 1); i >= (ll.size() - number); i--) {
            contents.append(ll.get(i - 1));
            contents.append("\n");
        }

        return contents.toString();
    }
}