How to read a file only 5 line and line in sort to descending. Example I have a file my.log with the following contents:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
And i want the result is as follows:
twelve
eleven
ten
nine
eight
My code now is :
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class MainActivity extends Activity {
long sleepTime = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String log = "/sdcard/my.log";
try {
Tail(log);
} catch (IOException e) {
e.printStackTrace();
}
}
public void Tail(String filename) throws IOException {
File checkfile = new File(filename);
if (checkfile.exists()) {
BufferedReader input = new BufferedReader(new FileReader(filename));
String currentLine = null;
while (true) {
if ((currentLine = input.readLine()) != null) {
Log.d("MyLog", currentLine);
continue;
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
input.close();
} else {
Log.d("MyLog", "File not found...");
}
}
}
But this time the result is all the content in the file in print and result not sort by descending. So now the result is as below:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
Thanks.