1

我在下面有这段代码,它读取下一行直到未编号文件的末尾(文件中的行没有数字),它工作得非常好。现在,我想读取前几行(向后读取)。如果也可能的话,随机播放(阅读随机行)。任何想法。

这是一个例子:

InputStream in;
BufferedReader reader;
String qline;

    try {
        in = this.getAssets().open("quotations.txt");       
        reader = new BufferedReader(new InputStreamReader(in));
        qline = reader.readLine();
        quote.setText(qline);

           } 
            catch (IOException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
                              }

在我的 onClick 方法中,我有一个下一步按钮

    //code
    else if (v.getId() == R.id.next) {


        try{
     if (( qline = reader.readLine()) != null) {
            // myData = myData + qline;


                  quote.setText(qline);
                          }

          } catch (java.io.FileNotFoundException e) {
            // do something if the myfilename.txt does not exits

          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 else if (v.getId() == R.id.back) {

// code for the back option

    }
4

1 回答 1

1

您可以使用我的库 FileReader,或对其进行修改以适应您寻求的功能:

// FileReader, Khaled A Khunaifer

public class FileReader
{
    public static ArrayList<String> readAllLines (String path)
    {
        ArrayList<String> lines = new ArrayList<String>();
        InputStream fis;
        BufferedReader br;
        String line;

        try
        {
            fis = new FileInputStream(path);
            br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));

            while ((line = br.readLine()) != null)
            {
                lines.add(line);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace()
        }

        return lines;
    }

    // NOTE: LINE NUMBERS START FROM 1
    public static ArrayList<String> readLines (String path, long from, long to)
    {
            ArrayList<String> lines = new ArrayList<String>();
        long k = 1;
        InputStream fis;
        BufferedReader br;
        String line;

        try
        {
            fis = new FileInputStream(path);
            br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));

            do
            {
                    line = br.readLine(); // read line k

                    if (k >= from)
                    {
                        if (k > to) break; // STOP

                        lines.add(line);
                    }

                k++;
            }
            while (line != null);
        }
        catch (Exception e)
        {
            e.printStackTrace()
        }

        return line;
    }

    // NOTE: LINE NUMBERS START FROM 1
    public static String readLine (String path, long i)
    {
        long k = 1;
        InputStream fis;
        BufferedReader br;
        String line;

        try
        {
            fis = new FileInputStream(path);
            br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));

            do
            {
                    line = br.readLine(); // read line k

                    if (k == i)
                    {
                        break;
                    }

                k++;
            }
            while (line != null);
        }
        catch (Exception e)
        {
            e.printStackTrace()
        }

        return line;
    }
}
于 2013-03-29T14:22:02.933 回答