2012-12-23 127 views
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 

    } 
+0

好吧,你可以使用'Math.Random'来输出任意数量的期望范围。一旦你有一个数字,移动fil e指针基于给定的随机数。 这是您可以关注的链接。 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html –

回答

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; 
    } 
}