android阅读器阅读txt文件

很早前就想做一个小说阅读器的。最近终于做了下来。 
其实很简单,这里只做了一小部分显示部分,还有标签,回翻的部分没有做。 
读取文件采用RandomAccessFile对大文件进行操作。 
早前使用read(byte[] b) 进行读取,可是总是有些意外,就是偶尔在一页的最后一行的几个字符出现乱码,导致下一页的前部分也会有乱码。而且这个问题很奇怪,自己在纯java环境中是没有任何的问题,可是只要在android虚拟机中就出现问题了。 
后来采用readLine()方法。 
一下是全部代码: 

package com.duduli.li;  
  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.RandomAccessFile;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.View.OnTouchListener;  
import android.widget.ScrollView;  
import android.widget.TextView;  
  
public class Ibook2Activity extends Activity {  
    private ScrollView sv;  
    private TextView tv;  
    private RandomAccessFile raf;  
    private int scrollViewY = 0;  
    private StringBuilder sb;  
    private long currentPoint;  
    private long fileLeght;  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
//        sv = (ScrollView) this.findViewById(R.id.scrollView);  
        tv = (TextView) this.findViewById(R.id.textView);  
        sv = (ScrollView) this.findViewById(R.id.scrollView);  
          
          
          
        File file = new File("/sdcard/zhuxian.txt");  
        try {  
            raf = new RandomAccessFile(file, "r");  
            fileLeght = raf.length();  
            System.out.println("文件总长度: "+fileLeght);  
            sb = new StringBuilder();  
            for(int i=0; i<=50;i++){  
                if(raf.getFilePointer() != fileLeght){  
//                  string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");  
                    sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));  
    //              break;  
            }else{  
                break;  
            }  
            }  
              
//          System.out.println(sb);  
            currentPoint = raf.getFilePointer();  
            System.out.println(currentPoint);  
            tv.setText(sb);  
//          tv.setText(string);  
              
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
          
        sv.setOnTouchListener(new OnTouchListener() {  
  
            @Override  
            public boolean onTouch(View v, MotionEvent event) {  
                // TODO Auto-generated method stub  
//              string = "";  
                if(event.getAction() == MotionEvent.ACTION_UP){  
                    if (sv.getScrollY() == 0) {    
                        System.out.println("up");  
                    } else if (sv.getScrollY() - scrollViewY < 2 && sv.getScrollY() >= scrollViewY) {    
                        try {  
                            raf.seek(currentPoint);  
                              
                            sb.delete(0, sb.length());  
                            for(int i=0; i<=50;i++){  
                                if(raf.getFilePointer() != fileLeght){  
//                                  string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");  
                                    sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));  
//                                  break;  
                                }else{  
                                    break;  
                                }  
                                  
                            }  
//                          System.out.println(string);  
                              
                            currentPoint = raf.getFilePointer();  
                            tv.setText(sb);  
//                          tv.setText(string);  
                            sv.scrollTo(0, 0);  
                              
                              
                        } catch (IOException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    } else {    
                        scrollViewY = sv.getScrollY();  
//                      System.out.println(sv.getScrollY());  
                    }    
                    System.out.println("currentPoint: "+currentPoint);  
                    System.out.println("ScrollY: "+sv.getScrollY());  
                      
                }  
                return false;  
            }  
              
        });  
    }  
      
      
      
      
}  

续:下面这个代码加入获得到达底部进行toast提示。 

package com.duduli.li;  
  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.RandomAccessFile;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.View.OnTouchListener;  
import android.widget.ScrollView;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class Ibook2Activity extends Activity {  
    private ScrollView sv;  
    private TextView tv;  
    private RandomAccessFile raf;  
    private int scrollViewY = 0;  
    private StringBuilder sb;  
    private long currentPoint;  
    private long fileLeght;  
    private boolean flag = true;  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
//        sv = (ScrollView) this.findViewById(R.id.scrollView);  
        tv = (TextView) this.findViewById(R.id.textView);  
        sv = (ScrollView) this.findViewById(R.id.scrollView);  
          
          
          
        File file = new File("/sdcard/test.txt");  
        try {  
            raf = new RandomAccessFile(file, "r");  
            fileLeght = raf.length();  
            System.out.println("文件总长度: "+fileLeght);  
            sb = new StringBuilder();  
            for(int i=0; i<=50;i++){  
                if(raf.getFilePointer() != fileLeght){  
//                  string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");  
                    sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));  
    //              break;  
            }else{  
                break;  
            }  
            }  
              
//          System.out.println(sb);  
            currentPoint = raf.getFilePointer();  
            System.out.println(currentPoint);  
            tv.setText(sb);  
//          tv.setText(string);  
              
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
          
        sv.setOnTouchListener(new OnTouchListener() {  
  
            @Override  
            public boolean onTouch(View v, MotionEvent event) {  
                // TODO Auto-generated method stub  
//              string = "";  
                if(event.getAction() == MotionEvent.ACTION_UP){  
                    if (sv.getScrollY() == 0) {    
                        System.out.println("up");  
                          
                      
                    } else if (sv.getScrollY() - scrollViewY < 2 && sv.getScrollY() >= scrollViewY) {    
                        try {  
                              
                            if(flag){  
                                System.out.println("begin");  
                                raf.seek(currentPoint);  
                                  
                                sb.delete(0, sb.length());  
                                for(int i=0; i<=50;i++){  
                                    if(raf.getFilePointer() != fileLeght){  
//                                      string += new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n");  
                                        sb.append(new String(raf.readLine().getBytes("8859_1"), "gb2312")+("\r\n"));  
//                                      break;  
                                    }else{  
                                          
//                                        
                                        flag = false;  
                                        break;  
                                    }  
                            }  
                              
                                  
                                  
//                          System.out.println(string);  
                              
                            currentPoint = raf.getFilePointer();  
                            tv.setText(sb);  
//                          tv.setText(string);  
                            sv.scrollTo(0, 0);  
                              
                            }  
                              
                              
                              
                        } catch (IOException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    } else {    
                        scrollViewY = sv.getScrollY();  
//                      System.out.println(sv.getScrollY());  
                    }    
                    if(!flag && (tv.getHeight() - sv.getScrollY() <= 362)){  
                            Toast.makeText(getApplicationContext(), "本文已结束",  
                                     Toast.LENGTH_SHORT).show();  
                    }  
                    System.out.println(tv.getHeight());  
                    System.out.println(sv.getScrollY());  
                      
                }  
                  
                return false;  
            }  
              
        });  
    }  
      
      
      
} 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。