Android代码在哪里开始,Android开始之 内部存储(示例代码)

1.保存文件在设备内部私有存储中,其他应用访问不到;

2.应用卸载后,,文件自动删

3.缓存

-----------------------------

0028b52c55fad0a58d004a6814ba6441.png

----------------------------测试---------------------------

5a8e130526933c2db5ac6fcdc5edf5d8.png

-------------------------------------

cd1d17b917c5701e6780a5a9c2859d3e.png

-----------------测试---------------------

1db44ad88071ece638f250eb700fbfba.png

------------------输入内容,,存入文件------------------------------

需要EditText和Button

9a80f8060afa8cabbacf333e6abdd216.png

----------------------------------保存到缓存文件---------------------------------------------------

1 //----------------保存到缓冲文件中----------------------

2 public boolean saveCacheFile(String filename,byte[]data) {3 boolean flag = false;4 File file =context.getFilesDir();5 FileOutputStream outputStream = null;6 try{7 File folderFile=new File(file.getAbsolutePath()+"/txt");//创建文件TXT目录8 if (!folderFile.exists()) {9 folderFile.mkdirs();//创建目录

10 }11 //outputStream = context.openFileOutput("my.txt",12 //context.MODE_WORLD_WRITEABLE);

13 outputStream=new FileOutputStream(folderFile.getAbsolutePath()+"/"+filename);14 outputStream.write(data,0,data.length);15 } catch(Exception e) {16 //TODO: handle exception

17 e.printStackTrace();18 } finally{19 if (outputStream != null) {20 try{21 outputStream.close();22 } catch(Exception e2) {23 //TODO: handle exception

24 }25

26 }27

28 }29 //System.out.println("---->>"+file.getAbsolutePath());30 //-->>/data/data/com.example.android_datastorage_internal/files

31

32 returnflag;33 }

---------------------------

1 //-------------遍历内容-------------------

2 public voidlistCacheFile(){3

4 //String[] strings =context.fileList();5 //for (String string:strings) {6 //System.out.println("----->>"+string);//列出文件夹:txt7 //}8 //遍历文件

9 File file=context.getFilesDir();10 File root=new File(file.getAbsolutePath()+"/txt");11 File[] listFile=root.listFiles();12 for(File file2:listFile) {13 System.out.println("---->>"+file2.getName());//列出文件夹里边的文件hello.txt

14

15 }16 }17 }

------测试----------------------------------------

1 public voidtest(){2 FileService service=newFileService(getContext());3 //service.saveCacheFile("hello.txt","你好".getBytes());

4 service.listCacheFile();5

6 }

---------------------------

6d9dd7bfa168234584cbba60df9a0ed6.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值