Android中byte数组生成文件并保存到手机

来源:互联网 发布:mac怎么建文本文件 编辑:程序博客网 时间:2024/05/16 19:52

新建一个android项目,在MainActivity中直接上代码:

package com.example.edittextresearch;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.widget.Toast;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initData();        // 假设这是从服务器上获取的数据,或者其他的什么地方获取到的。        byte[] bytes = "hello world".getBytes();        // 根据byte数组生成文件,保存到手机上        createFileWithByte(bytes);        // 弹出信息提示        Toast.makeText(MainActivity.this, "生成文件成功!", Toast.LENGTH_LONG).show();    }    private String fileName;// 文件命名    private void initData() {        // TODO Auto-generated method stub        fileName = "byte_to_file.doc";    }    /**     * 根据byte数组生成文件     *      * @param bytes     *            生成文件用到的byte数组     */    private void createFileWithByte(byte[] bytes) {        // TODO Auto-generated method stub        /**         * 创建File对象,其中包含文件所在的目录以及文件的命名         */        File file = new File(Environment.getExternalStorageDirectory(),                fileName);        // 创建FileOutputStream对象        FileOutputStream outputStream = null;        // 创建BufferedOutputStream对象        BufferedOutputStream bufferedOutputStream = null;        try {            // 如果文件存在则删除            if (file.exists()) {                file.delete();            }            // 在文件系统中根据路径创建一个新的空文件            file.createNewFile();            // 获取FileOutputStream对象            outputStream = new FileOutputStream(file);            // 获取BufferedOutputStream对象            bufferedOutputStream = new BufferedOutputStream(outputStream);            // 往文件所在的缓冲输出流中写byte数据            bufferedOutputStream.write(bytes);            // 刷出缓冲输出流,该步很关键,要是不执行flush()方法,那么文件的内容是空的。            bufferedOutputStream.flush();        } catch (Exception e) {            // 打印异常信息            e.printStackTrace();        } finally {            // 关闭创建的流对象            if (outputStream != null) {                try {                    outputStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (bufferedOutputStream != null) {                try {                    bufferedOutputStream.close();                } catch (Exception e2) {                    e2.printStackTrace();                }            }        }    }}
  • 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
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

我在代码中都加了注释,大家看起来也很简单。大概讲讲:


  • 首先需要获取byte类型数组数据,这里我是用的假设的数据,实际中大家发送网络请求获取即可;
  • 数据拿到后就可以将其写到文件缓冲输出流中,然后调用flush()方法刷出流对象,这样文件里面就有内容了。

下面附上我的手机上文件的保存路径图片和打开后的内容。如下所示:


1> 文件存到手机上的图片如下所示:

这里写图片描述

2> 选择文件的打开方式如下图所示:

这里写图片描述

3> 文件打开后的显示如下图所示:

这里写图片描述