如何从文件中对降序内容进行排序,只获取5行

时间:2013-08-15 18:55:47

标签: java android java-io

如何只读取5行和行中的行以降序。 示例我有一个文件my.log,其中包含以下内容:

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

我希望结果如下:

twelve
eleven
ten
nine
eight

我的代码现在是:

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...");
        }
    }

}

但这次结果是打印文件中的所有内容,结果不按降序排序。所以现在的结果如下:

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

感谢。

7 个答案:

答案 0 :(得分:2)

您需要先缓存List中的所有行。然后你可以得到最后五行。

List<String> l = new List<String>();
...
l.add(currentLine);
...
for(int i = 0; i < 5; i++){
    Log.d("MyLog", l.get(l.size() - i - 1));
}

答案 1 :(得分:1)

维护一个包含5个字符串的数组,并循环填充文件行。然后只按相反的顺序枚举:

int MAX_LINES_COUNT = 5;
String[] lastFiveLines = new String[MAX_LINES_COUNT];    
int lineNumber = 0;

// populate
// .... inside the loop

         lastFiveLines[lineNumber++ % MAX_LINES_COUNT] = currentLine;

//....
// Now get the last five lines

for(int i=0; i<MAX_LINES_COUNT; i++)
{
    Log.d("MyLog", lastFiveLines[(--lineNumber) % MAX_LINES_COUNT]);
}

只需要必要的内存量(适用于大文件)。

答案 2 :(得分:0)

如果您知道您的文件是按排序顺序排列的,则无需对其进行排序?

一次读取一行文件。保持读取最后5行的窗口,并读取整个文件。

最后,您将按升序排列文件的最后五行。现在翻转它们。现在你已经按降序排列了它们。

如果您需要按顺序排列任何行,只需将文件读入RAM并迭代您需要的范围。

答案 3 :(得分:0)

如果文件足够大,则读取整个文件会引发性能问题。

为了提高效率,您需要使用RandomAccessFile类,然后向后读取文件(将文件指针移动到最后一个字符,然后回过头直到获得5行)。

当您需要只读取或操作文件内容的一部分时,RandomAccessFile就是您的选择。

答案 4 :(得分:0)

逐行读取您的文件,但只保留最后五行,使用LinkedList并在开头添加最后一行读取行。如果在每行之后列表超过五行,则删除第五行:

BufferedReader reader = new BufferedReader(new FileReader(myLog));
String line;
while((line = reader.readLine()) != null) {
    lastLines.add(0, line);
    if(lastLines.size() > 5)
        lastLines.remove(5);
}

// lastLines will have your five lines in reversed order
System.out.println(lastLines);

答案 5 :(得分:0)

使用RandomAccessFile是最简单的解决方案之一。指向文件末尾,然后向后阅读。这个thread列出了它的源代码。

答案 6 :(得分:0)

我无法弄清楚你使用Thread的原因。我将尝试仅使用Java I / O和Util来回答您的问题。所以,如果这不适合,请使用等效的android dev包。

public void tail(File in){
    List<String> list = new ArrayList<String>();\\ You could use an Linked List as well
    try {
    Scanner scanner = new Scanner(in).useDelimiter("\n");

    while(scanner.hasNext()){
    String line = scanner.next().trim();
    list.add(line);
    scanner.close();
} 
     catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
    for(int i=list.size()-1; i>list.size()-6; i--){
        System.out.println(list.get(i));
    }
}