博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android的读写文件及权限设置
阅读量:6114 次
发布时间:2019-06-21

本文共 1890 字,大约阅读时间需要 6 分钟。

 

drwx

  read

  write

  excute

openFileOutput(name,drwx); 用系统api读取文件

 

 

设置文件生成的权限:

    public static boolean saveInfo(  
                Context context, String userName, String userPass, int mode){              

            try {  

                FileOutputStream fos;  
                switch (mode) {  
                case 0:  
                    fos = context.openFileOutput(  
                            "private.txt", Context.MODE_PRIVATE);  
                    fos.write((userName+"##"+userPass).getBytes());  
                    fos.close();  
                    break;  
                case 1:  
                    fos = context.openFileOutput(  
                            "readable.txt", Context.MODE_WORLD_READABLE);  
                    fos.write((userName+"##"+userPass).getBytes());  
                    fos.close();  
                    break;  
                case 2:  
                    fos = context.openFileOutput(  
                            "writeable.txt", Context.MODE_WORLD_WRITEABLE);  
                    fos.write((userName+"##"+userPass).getBytes());  
                    fos.close();  
                    break;  
                case 3:  
                    fos = context.openFileOutput(  
                            "public.txt", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);  
                    fos.write((userName+"##"+userPass).getBytes());  
                    fos.close();  
                    break;  
                default:  
                    break;  
                }  
                  
                  
                return true;  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            }  
        } 

 

下面是读取文件和写入文件的程序代码:

读取文件:
    public void readInfo(View view){  
            File file = new File("/data/data/com.aaron.login/files/public.txt");  
            FileInputStream fis;  
            try {  
                fis = new FileInputStream(file);  
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));  
                String result = br.readLine();  
                Toast.makeText(MainActivity.this,  
                        result,   
                        Toast.LENGTH_LONG).show();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
                Toast.makeText(MainActivity.this,  
                        "读取文件失败",  
                        Toast.LENGTH_LONG).show();  
            }  
        } 

 

写入文件:

    public void writeInfo(View view){  
            File file = new File("/data/data/com.aaron.login/files/public.txt");  
            FileOutputStream fos;  
            try {  
                fos = new FileOutputStream(file);  
                fos.write("hahaha".getBytes());  
                fos.close();  
                Toast.makeText(MainActivity.this,  
                        "写入文件成功",   
                        Toast.LENGTH_LONG).show();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
                Toast.makeText(MainActivity.this,   
                        "写入文件失败",   
                        Toast.LENGTH_LONG).show();  
            }  

              
        } 

你可能感兴趣的文章
Windows 原生运行 Linux 的技术细节
查看>>
《Abaqus GUI程序开发指南(Python语言)》——2.12 文件的操作
查看>>
《Adobe Premiere Pro CC经典教程》——第14 课 颜色校正和分级 14.1 开始
查看>>
Nologging到底何时才能生效?
查看>>
SoftEther ***
查看>>
我的友情链接
查看>>
activity以dialog形式显示
查看>>
用vcenter converter从vmwave workstation clone CentOS去ESXi 需更改网卡MAC
查看>>
防止刚进入activity的时候,edittext获取焦点,自动弹出软键盘
查看>>
【白话设计模式十一】生成器模式(Builder)
查看>>
exchange 2010 反垃圾邮件
查看>>
MySql-统计所有会员的最高前10次的积分和
查看>>
df du 磁盘分区 mkfs mount
查看>>
log4j配置详解与使用方法说明
查看>>
springmvc入门
查看>>
Android--------TabLayout实现新闻客户端顶部导航栏
查看>>
排毒,美容,什么时间最好。
查看>>
Exchange小技巧一则:部门、客服等专用邮箱
查看>>
Go 1.11 的go.mod
查看>>
关于sql性能一个小技巧
查看>>