获取assets目录下的文件,写到一个新的file里面去

private void writeAssetFile2File() {
        String fileName = "test.txt";
        File file= new File(sdRootFile, fileName);
        try {
            InputStream is = getAssets().open(fileName);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int byteCount = 0;
            while ((byteCount = is.read(buffer)) != -1) {//循环从输入流读取 buffer字节
                fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
            }
            fos.flush();//刷新缓冲区
            is.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的