Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort descending contents from file and get it only 5 line
    text
    copied!<p>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:</p> <pre><code>one two three four five six seven eight nine ten eleven twelve </code></pre> <p>And i want the result is as follows:</p> <pre><code>twelve eleven ten nine eight </code></pre> <p>My code now is :</p> <pre><code>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..."); } } } </code></pre> <p>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:</p> <pre><code>one two three four five six seven eight nine ten eleven twelve </code></pre> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload