这是一个把记事本中的内容读取到内存中并将内容显示在屏幕上的demo。在这个demo中,整个程序的流程是这样:
step 1:首先在程序的assets文件夹下存放一个test.txt文件,文件是utf-8编码格式,里面存放的是一篇文章。windows下生成的记事本默认是ANSI编码格式,你只需要点击 文件 --> 另存为 --> 在弹出框中把编码格式改成utf-8 --> 保存覆盖原文件即可。
step 2:检测sdcard是否插入,false则输出提示:"sdcard未插入。",true则执行step3。
step 3:检测sdcard中mana文件夹下是否有test.txt文件,如果有则读取文件内容,如果没有则从assets中把test.txt文件拷贝到sdcard的mana文件夹下,然后读取sdcard中的test.txt文件内容。
step 4:把文件内容显示到设备屏幕上大功告成。
显示效果如下:
这个是布局main.xml的内容,只有一个textView,在textView外层包了一个scrollView,使得当文字多于屏幕能够显示的能力时可以拖动文字。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@id/text_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</RelativeLayout>
Main.class,这是显示的Activity,其中最关键的是对MappedByteBuffer类的掌握。
/**
* 从SDCard中读取文本内容
* @author haozi
*
*/
public class Main extends Activit