android写入文件读取文件

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

 
 
<LinearLayout



    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="net.bwie.localdata.activity.FileActivity">

    <Button
        android:id="@+id/read_file_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取文件"/>

    <Button
        android:id="@+id/write_file_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入文件"/>

    <TextView
        android:id="@+id/result_tv"
        android:text="结果"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

public class FileActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button mReadFileBtn;
    protected Button mWriteFileBtn;
    protected TextView mResultTv;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, FileActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_file);
        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.read_file_btn) {
            String result = readFile();
            mResultTv.setText(result);

        } else if (view.getId() == R.id.write_file_btn) {
            writeFile();
        }
    }

    // 读取文件
    private String readFile() {
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/abc/";
        String fileName = "xyz.txt";

        File file = new File(filePath, fileName);

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String result = "";
            String line = "";

            while ((line = br.readLine()) != null) {
                result += line;
            }
            return result;


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    // 写入文件
    private void writeFile() {

        // 外部存储私有路径:Android文件夹
//        String privatePath = getExternalFilesDir(null).getPath();// 私有路径不分类为null
//        String filePath = privatePath + "/abc/";

        // 外部存储公共路径:DICM,DOWNLOAD,MUSIC等系统提供的文件夹
//        String publicPath = Environment
//                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
//                .getPath();
//        String filePath = publicPath + "/abc/";

        // 自定义文件路径
        String rootPath = Environment.getExternalStorageDirectory().getPath();// 外部存储路径(根目录)
        String filePath = rootPath + "/abc/";

        String fileName = "xyz.txt";

        File file = new File(filePath, fileName);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write("asdasdas".getBytes());
            Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("1507", "error: " + e.getMessage());
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private void initView() {
        mReadFileBtn = (Button) findViewById(R.id.read_file_btn);
        mReadFileBtn.setOnClickListener(FileActivity.this);
        mWriteFileBtn = (Button) findViewById(R.id.write_file_btn);
        mWriteFileBtn.setOnClickListener(FileActivity.this);
        mResultTv = (TextView) findViewById(R.id.result_tv);
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值