android文件存储:直接存储文件和文件存储到SDCard

之前我们的内容是存在手机自带的存储空间里的,但是我们知道手机自带的存储空间比较小,如果我们要存储的内容,比如说视频占空间很大,我们就必须得将大文件存入SDCard。

1.直接存储,文件存入/data/data/<package name>/files目录下

FileService.java   保存文件方法代码

1
2
3
4
5
6
7
//直接存储文件

 public void save(String fileName, String content) throws Exception {

        //文件输出流

         FileOutputStream fos;
         fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
         fos.write(content.getBytes());
         fos.close();
     }

单元测试存储:

1

FileService  service=new FileServie();

service.save("aa.txt""sfdgdhfh");

存储成功后的aa文件存到data/data/项目/files下的aa.txt,导出文件查看内容是:sfdgdhfh

 

2.存入SDCard

         用户输入文件名及内容点击保存按钮存储到SDCard

        布局代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"
      xmlns:tools = "http://schemas.android.com/tools"
      android:layout_width = "fill_parent"
      android:layout_height = "match_parent"
      android:orientation = "vertical"   >
                                                            
      < TextView
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "文件名称"
          tools:context = ".MainActivity"   />
                                                            
      < EditText
          android:id = "@+id/filename"
          android:layout_width = "fill_parent"
          android:layout_height = "wrap_content"
          android:inputType = "text"   />
                                                            
      < TextView
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "文件内容"   />
                                                            
      < EditText
          android:id = "@+id/content"
          android:layout_width = "fill_parent"
          android:layout_height = "wrap_content"
          android:gravity = "top"
          android:minLines = "4"
          android:inputType = "textMultiLine"   />
                                                            
      < Button
          android:id = "@+id/save"
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "保存"
          tools:context = ".MainActivity"   />
                                                            
  </ LinearLayout >

编写业务类FileService,实现存储功能saveToSDCard()及读文件readFile()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public   class   FileService {
     private   Context context;
                                                    
     public   FileService(Context context) {
         super ();
         this.context = context;
     }
     public   void   saveToSDcard(String filename, String content)  throws   IOException{
        //创建文件file及得到外部存储设备绝对路径
        File file= new   File(Environment.getExternalStorageDirectory(),filename);
        //文件输出流
        FileOutputStream fos= new   FileOutputStream(file);
        //通过文件输出流写入文件内容
        fos.write(content.getBytes());
        fos.close();
     }
                                                      
     // 读文件
     public   String readFile(String filename)  throws   IOException {
         FileInputStream fis = context.openFileInput(filename);
         int   len =  0 ;
         byte [] buffer =  new   byte [ 1024 ];
         // 往内存中输出数据
         //另一种输出方式  StringBuffer sbuffer=new StringBuffer();
         ByteArrayOutputStream outstream =  new   ByteArrayOutputStream();
         while   ((len = fis.read(buffer)) != - 1 ) {
             //sbuffer.append(buffer);
             outstream.write(buffer,  0 , len);
         }
         // 得到内存中数据以二进制存放
         byte [] s=outstream.toByteArray();     
         outstream.close();
         fis.close();
         return   new   String(s); 
     }
  }

编写测试类测试存储方法是否有错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public   class   FileServiceTest  extends   AndroidTestCase {
     FileService service =  new   FileService(getContext());
     public   void   testSaveToSDcard() {
         try   {
             service.saveToSDcard( "b.txt" "文件存储到SDCard,真好" );
         catch   (IOException e) {
             e.printStackTrace();
         }
     }
                                               
     public   void   testReadFile() {
         try   {
             System.out.println(service.readFile( "b.txt" ));
         catch   (IOException e) {
             e.printStackTrace();
         }
                                                      
     }
                                               
  }

单元测试应配置一些属性

1
2
<!-- 单元测试库的配置 -->
          < uses-library   android:name = "android.test.runner"   />
1
2
3
4
5
<!-- 单元测试 -->
      < instrumentation
          android:name = "android.test.InstrumentationTestRunner"
          android:targetPackage = "cn.bzu.filefro"   >
      </ instrumentation >

Activity代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public   class   MainActivity  extends   Activity {
     private   EditText filename;
     private   EditText content;
     private   Button save;
     @Override
     public   void   onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        filename=(EditText)  this .findViewById(R.id.filename);
        content=(EditText)  this .findViewById(R.id.content);
         save=(Button)  this .findViewById(R.id.save);
         save.setOnClickListener( new   View.OnClickListener() {
                                             
             public   void   onClick(View v) {
                 String file=filename.getText().toString();
                 String filecontent=content.getText().toString();
                 FileService service=new FileService(MainActivity.this);
                 try   {
                     service.saveToSDcard(file, filecontent);
                     System.out.println(service.readFile(file));
                     Toast.makeText(MainActivity. this , file+ "存储成功\n内容是:" +filecontent, Toast.LENGTH_LONG).show();
                                                     
                 catch   (IOException e) {
                     e.printStackTrace();
                 }
             }
         });
     }
}


效果图




 

文件存储到mnt/sdcard/下


导出hello内容是:

大家好,见到你们很高兴,嘿嘿。希望以后相处融洽。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值