ANDROID笔记:File操作

package com.example.android_file;



import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;



import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends Activity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View arg0) {

                // write("测试字符串");

                writeSDFile("测试字符串");

            }

        });

        Button button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View arg0) {

                // Toast.makeText(MainActivity.this, read(), 200).show();

                Toast.makeText(MainActivity.this, readSDFile(), 200).show();

            }

        });

    }



    /**

     * 向程序作用域文件写入内容,路径:/data/data/com.example.android_file/files

     * 

     * @param content

     */

    private void write(String content) {

        try {

            FileOutputStream fileOutputStream = openFileOutput("file.txt",

                    Activity.MODE_APPEND);

            PrintStream printStream = new PrintStream(fileOutputStream);

            printStream.println(content);

            printStream.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }



    }



    /**

     * 获取程序作用域文件的内容,路径:/data/data/com.example.android_file/files

     * 

     * @return

     */

    private String read() {

        StringBuffer stringBuffer = new StringBuffer();

        FileInputStream fileInputStream = null;

        try {

            // openFileInput

            fileInputStream = openFileInput("file.txt");

            byte[] bytes = new byte[1024];

            int len = 0;

            while ((len = fileInputStream.read(bytes)) != -1) {

                stringBuffer.append(new String(bytes, "utf-8"));



            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                fileInputStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return stringBuffer.toString();



    }



    /**

     * 向SD文件写入内容

     * 

     * @param content

     */

    private void writeSDFile(String content) {

        if (Environment.getExternalStorageState().equals(

                Environment.MEDIA_MOUNTED)) {

            try {

                // 获取sd的路径

                String path = Environment.getExternalStorageDirectory()

                        .getCanonicalPath() + "/file.txt";

                FileOutputStream fileOutputStream = new FileOutputStream(path,

                        true);// true:追加方式

                PrintStream printStream = new PrintStream(fileOutputStream);

                printStream.println(content);

                printStream.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }



    }



    /**

     * 获取SD卡文件内容

     * 

     * @return

     */

    private String readSDFile() {

        StringBuffer stringBuffer = new StringBuffer();

        FileInputStream fileInputStream = null;

        if (Environment.getExternalStorageState().equals(

                Environment.MEDIA_MOUNTED)) {

            // 获取sd的路径

            try {

                String path = Environment.getExternalStorageDirectory()

                        .getCanonicalPath() + "/file.txt";

                fileInputStream = new FileInputStream(path);

                byte[] bytes = new byte[1024];

                int len = 0;

                while ((len = fileInputStream.read(bytes)) != -1) {

                    stringBuffer.append(new String(bytes, "utf-8"));

                }

            } catch (IOException e) {

                e.printStackTrace();

            } finally {

                try {

                    fileInputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }



        }

        return stringBuffer.toString();

    }

}
openFileOutput和openFileInput 只能用于/data/data/包名/files 下文件的操作

你可能感兴趣的:(android)