File 存储

FileInputStream FileOutputStream
Context提供了如下两个方法来打开应用程序的数据文件夹里的文件IO流
1).FileInputStream openFileInput(String name):打开应用程序的数据文件夹下的name文件对应的输入流
2).FileOutputStream openFileOutput(String name, int mode):打开应用程序的数据文件夹下的name文件对应的输出流
第二个方法的第二个参数为指定文件的打开模式:
MODE_PRIVATE:该文件只能被当前的程序读写
MODE_APPEND:以追加方式打开该文件,应用程序可以向该文件中追加内容
MODE_WORLD_READABLE:该文件的内容可以被其他程序读取
MODE_WORLD_WRITEABLE:该文件的内容可由其他程序读写
Android 4.2开始,android 不推荐使用MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE.
private String read(){
//打开文件输入流
try {
FileInputStream fis =openFileInput(“zhaoyi”);
byte [] buff =new byte[1024];
int hasRead=0;
StringBuilder sb=new StringBuilder(“”);
//读取文件内容
while ((hasRead=fis.read(buff))>0) {
sb.append(new String(buff, 0, hasRead));

        }
        fis.close();
        Log.i("zhaoyi11","sb  "+sb.toString());
        return sb.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    return null;
}
private void write(String content){
    Log.i("zhaoyi11","content  "+content);
    //以追加方式打开文件输出流
    try {
        FileOutputStream fos=openFileOutput("zhaoyi", MODE_APPEND);
        //将FileOutputStream 包装成PrintStream
        PrintStream pS=new PrintStream(fos);
        //输出文件内容
        pS.println(content);
        pS.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值