Andriod 文件(File) 使用

  1. 文件操作,首先需要一个文件路径(FilePath)

    .AndroidStudio -> Device File Explorer上查找文件夹
    
    1. Context.getFilesDir().getAbsolutePath()+ File.separator + "user.txt"  
    	对应路径是 /data/user/0/包名/files/user.txt
    	
    2. Context.openFileInput("user.txt",MODE_PRIVATE)Context.openFileOutput()
    	该方法返回 FileInputStreamFileOutputStream对象 
    	对应路径是 /data/user/0/包名/files/user.txt
    	
    3. Context.getCacheDir().getAbsolutePath() + File.separator + "user.txt"
    	对应路径是 /data/user/0/包名/cache/user.txt
    	
    4. Context.getDir("myfile",MODE_PRIVATE).getAbsolutePath() + File.separator + "user.txt"
    	对应路径是 /data/user/0/包名/app_myfile/user.txt	
    	创建文件的权限
    	MODE_PRIVATE:只能被当前的应用程序 读写
    	MODE_APPEND: 其他应用程序也可以向该文件中 追加内容
    	MODE_WORLD_READABLE:可被其他的应用程序 读取
    	MODE_WORLD_WRITEABLE:可被其他的应用程序 读写
    	
    4. Context.fileList() 
    	返回/data/user/0/包名/files/ 下的所有文件名(String[]5. Context.deleteFile(String) 
    	删除/data/user/0/包名/files/ 下指定文件
    
    二.在手机文件管理中 Android 文件夹下找
    
    1. Context.getExternalFilesDir().getAbsolutePath()+ File.separator + "user.txt"
    	返回 /storage/emulated/0/Android/data/包名/files/myfile/user.txt
    	
    2. Context.getExternalCacheDir().getAbsolutePath() + File.separator + "user.txt"
    	返回 /storage/emulated/0/Android/data/包名/cache/user.txt
    
    
  2. demo实例

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        String userPath = getDir("myfile",MODE_PRIVATE).getAbsolutePath() + File.separator + "user.txt";
        Log.e("zjq", "onCreate: " + userPath);
    
        findViewById(R.id.btn_save).setOnClickListener(v -> save(userPath));
    
        findViewById(R.id.btn_get).setOnClickListener(v -> get(userPath));
    }
    
    
    private void save(String userPath) {
        new Thread(() -> {
    
            User user = new User("zjq");
    
            File file = new File(userPath);
            ObjectOutputStream objectOutputStream = null;
            try {
            //   objectOutputStream = new ObjectOutputStream(openFileOutput("myfile.txt",MODE_PRIVATE));
                objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
                objectOutputStream.writeObject(user);
        
                Log.e("zjq", "save: " + user.getName());
    
            } catch (IOException e) {
                Log.e("zjq", "save:IOException: " + e.getMessage());
            } finally {
                try {
                    if (objectOutputStream != null) {
                        objectOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }).start();
    }
    
    private void get(String userPath) {
    
        new Thread(() -> {
    
            File file = new File(userPath);
    
            if (file.exists()) {
                ObjectInputStream inputStream = null;
                try {
                   //inputStream = new ObjectInputStream(openFileInput("myfile.txt"));
                   inputStream = new ObjectInputStream(new FileInputStream(file));
                   User user = (User) inputStream.readObject();
                    
                    String name = user.getName();
                    Log.e("zjq", "get: " + name);
                    runOnUiThread(() -> {
                        TextView textView = findViewById(R.id.tv_name);
                        textView.setText(name);
                    });
    
                } catch (IOException | ClassNotFoundException e) {
                    Log.e("zjq", "get:Exception: " + e.getMessage());
                } finally {
                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }).start();
    }
    
    }
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值