• android 文件存储


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

    package net.bwie.localdata.activity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import net.bwie.localdata.R;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    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);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <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>
    

      

  • 相关阅读:
    js冒泡排序
    HTML5 canvas 计时器
    centos 6.4安装杀毒软件clamAV 0.98[转]
    PHP大文件下载
    如何在 Eclipse 中使用插件构建 PHP 开发环境[转]
    CentOS 单用户模式:修改Root密码和grub加密[转]
    CentOS 6.0 VNC远程桌面配置[转]
    gprof使用介绍 (gcc -pg) [转]
    VMware NAT端口映射外网访问虚拟机linux
    shell判断文件是否存在[转]
  • 原文地址:https://www.cnblogs.com/yudada/p/7881291.html
Copyright © 2020-2023  润新知