Android中的文件读写

InputStream 对应读
OutputStream 对应写

1、从Assets里读取文件

/**
     * 读取Assets里的文件,返回字符串
     * @param context
     * @param fileName
     * @return
     */
public static String readAssets(Context context, String fileName) {
        try {
            InputStream inputStream = context.getAssets().open(fileName);
            return streamToString(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String streamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

2、保存文件

/**
     * 保存文件
     *
     * @param inputStream
     * @param folder      文件所在的文件夹
     * @param fileName    文件名
     **/
    public static boolean saveFile(InputStream inputStream, String folder, String fileName) {
        boolean isSuccess = false;
        InputStream is = null;
        FileOutputStream fos = null;

        try {
            is = inputStream;
            long sum = 0;
            File dir = new File(folder);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            File file = new File(dir, fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            byte[] buf = new byte[2048];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
                sum += len;
                fos.write(buf, 0, len);
            }
            fos.flush();
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        } finally {
            try {
                is.close();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isSuccess;
    }

3、复制文件

 /**
     * 复制文件
     *
     * @param from 要复制的文件
     * @param to   要写入的文件
     * @return true 成功
     **/
    public static boolean copyFile(File from, File to) {
        boolean isSuccess = false;

        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream(from);
            out = new FileOutputStream(to);
            byte[] bytes = new byte[1024];
            int count = 0;
            while ((count = in.read(bytes)) != -1) {
                out.write(bytes, 0, count);
                out.flush();
            }
            isSuccess = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(in != null) {
                   in.close();
                }
               if(out != null) {
                  out.close();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isSuccess;
    }

你可能感兴趣的:(Android中的文件读写)