Java 文件存储 - litonghui/TechBlog GitHub Wiki

Java 文件存储方法

1.文件copy 方法,将现有文件copy 到指定文件路径。

        public void onFileCopy(File resource) {
            if (resource != null) {
                InputStream inputStream;
                OutputStream outputStream;
                try {
                    inputStream = new FileInputStream(resource);
                    File dir = new File(BASEPATH);
                    if (!dir.exists())
                        dir.mkdirs();
                    String targetPath = new StringBuilder(BASEPATH).append(File.separator)
                            .append(resource.getName()).toString();
                    outputStream = new FileOutputStream(targetPath);
                    byte[] buffer = new byte[1024];
                    int read;
                    while ((read = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, read);
                    }
                    inputStream.close();
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

2.文件下载到指定文件路径。(借助volley)

    NetworkRequest.getImageLoader().get(mImageEntity.image, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer imageContainer, boolean isImmediate) {
            Bitmap bitmap = imageContainer.getBitmap();
            if(bitmap!=null) {

                try {                  
                    File dir = new File(BASEPATH);
                    if (!dir.exists())
                        dir.mkdirs();

                    String targetPath = BASEPATH+ File.separator;

                    File f = new File(targetPath, "1.png");

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 0 *//*ignored for PNG*//*, bos);
                    byte[] bitmapdata = bos.toByteArray();

                    FileOutputStream fos = null;

                    fos = new FileOutputStream(f);
                    fos.write(bitmapdata);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });*/

3,创建文件夹并存储:

遇到一个bug,在创建文件夹并存储出现一异常,open failed: ENOENT (No such file or directory),错误代码如下:
private void createRecordDir() {
    File sampleDir = new File(Environment.getStorageDirectory() + File.separator
            + "/lth/video/");
    if (!sampleDir.exists())
        sampleDir.mkdir();
    try {
        mRecordFile = File.createTempFile("recording", ".mp4", sampleDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
查找原因是,在FileOutputStream创建一个流文件路径时文件应该存在,createTempleFile 对文件操作时不可以对文件直接操作,需要创建临时文件。如:
private void createRecordDir() {
    File sampleDir = new File(Environment.getExternalStorageDirectory()
            + File.separator + "lth/video/");//录制视频的保存地址
    if (!sampleDir.exists()) {
        sampleDir.mkdirs();
    }
    File vecordDir = sampleDir;
    // 创建文件
    try {
        mRecordFile = File.createTempFile("recording", ".mp4", vecordDir);// mp4格式的录制的视频文件
    } catch (IOException e) {
        e.printStackTrace();
    }
}