Android 学习笔记 文本文件的读写操作

activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/btn01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存" />
    <Button
        android:id="@+id/btn02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="读取" />
    
    <TextView
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

 

MainActivity.java

public class MainActivity extends Activity {
    private static final String FILENAME="Test01.txt";
    private Button btn01=null;
    private Button btn02=null;
    private TextView msg=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        this.btn01=(Button)super.findViewById(R.id.btn01);
        this.btn02=(Button)super.findViewById(R.id.btn02);
        this.btn01.setOnClickListener(new OnClickListenerImpl());
        this.btn02.setOnClickListener(new OnClickListenerImpl());
        this.msg=(TextView)super.findViewById(R.id.msg);
    }
    private class OnClickListenerImpl implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.btn01:
                FileOutputStream output=null;
                try {
                    output=MainActivity.this.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                PrintStream out=new PrintStream(output);
                out.println(sdf.format(new Date()));
                out.println("文本内容aa");
                out.close();
                Toast.makeText(MainActivity.this, "文件已经保存", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn02:
                FileInputStream input=null;
                try {
                    input=MainActivity.this.openFileInput(FILENAME);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Scanner scanner=new Scanner(input);
                MainActivity.this.msg.setText("");
                MainActivity.this.msg.append("-------Start--------\n");
                while (scanner.hasNext()) {
                    MainActivity.this.msg.append(scanner.next()+"\n");
                }
                MainActivity.this.msg.append("-------End--------\n");
                Toast.makeText(MainActivity.this, "文件已经读取", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 如果需要操作SD卡上的文件,需要在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

 

posted @ 2013-10-18 09:53  寂静之秋  阅读(770)  评论(0编辑  收藏  举报
哈尔滨八零网