Android开始之 内部存储

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

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

3.缓存

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

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

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

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

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

需要EditText和Button

 

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

 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         return flag;
33     }

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

 1     //-------------遍历内容-------------------
 2     public void listCacheFile(){
 3         
 4 //        String[] strings =context.fileList();
 5 //        for (String string:strings) {
 6 //            System.out.println("----->>"+string);//列出文件夹:txt
 7 //        }
 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 void test(){
2         FileService service=new FileService(getContext());
3         //service.saveCacheFile("hello.txt","你好".getBytes());
4         service.listCacheFile();
5         
6     }

 

 

 

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

 

 

posted @ 2017-03-30 16:43  张兮兮  阅读(226)  评论(0编辑  收藏  举报