• 13_文件的操作模式


    私有文件访问测试

    package cn.itcast.test;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    
    import android.test.AndroidTestCase;
    import android.util.Log;
    
    public class AccessOtherAppPrivateTest extends AndroidTestCase {
        
        public void testAccessPrivate() throws Throwable{
            
            String path = "/data/data/cn.itcast.files/files/itcast.txt";
            File file = new File(path);
            FileInputStream inStream = new FileInputStream(file);
            
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len=inStream.read(buffer))!=-1){
                outStream.write(buffer, 0, len) ;
            }
            
            byte[] data = outStream.toByteArray();
            outStream.close();
            inStream.close();
            String content = new String(data);
            Log.i("test", content);
        }
    }

     扩展保存

        public void saveAppend(String filename, String content) throws Exception{
            // TODO Auto-generated method stub
            //    MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容
            FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
            outStream.write(content.getBytes());
            outStream.close();
        }

     可读

        public void saveReadable(String filename, String content) throws Exception{
            // TODO Auto-generated method stub
            //    MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容
            FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
            outStream.write(content.getBytes());
            outStream.close();
        }

    读写

        public void saveRW(String filename, String content) throws Exception{
            // TODO Auto-generated method stub
            //    MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容
            FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
            outStream.write(content.getBytes());
            outStream.close();
        }    
  • 相关阅读:
    安全参考和书安 NPM、PYPI、DockerHub 备份
    [NTT][组合]P4491 [HAOI2018]染色
    [主席树][哈希][最短路]CF464E The Classic Problem
    序章在那星雨之夜
    [二分][后缀数组]luogu P2463 [SDOI2008]Sandy的卡片
    GDOI 2021 退役记
    [后缀数组][单调栈]luogu P4248 [AHOI2013]差异
    [BFS]luogu P2536 [AHOI2005]病毒检测
    [多项式下降幂][组合][二项式定理]luogu P6620 [省选联考 2020 A 卷] 组合数问题
    AtCoder Regular Contest 116 总结
  • 原文地址:https://www.cnblogs.com/carl2380/p/4159547.html
Copyright © 2020-2023  润新知