从资产中获取一个文件,读取它并将内容复制到另一个文件中

Get a file from assets, read it and copy the contents in another file

我在资产文件夹中有一个文件。我想打开文件,通过 InputStream 读取 byte[] 然后使用 FileOutputStream 将 byte[] 写入另一个文件。

我的代码:

assetManager = MainActivity.this.getAssets();
                assetStream = assetManager.open("qamaster2.pfx");
                File file = StringGenerator.createFileFromInputStream(assetStream, "qamaster2.pfx");

和方法 createFileFromInputStream:

public static File createFileFromInputStream(InputStream inputStream, String fileName) {

        try{
            File file = new File(fileName);
            Log.e("File", file.getName());
            if (file.exists()){
                int size = inputStream.available();
                Log.e("File size", String.valueOf(size));
                byte[] buffer = new byte[size];
                inputStream.read(buffer);
                inputStream.close();
            }
            return file;
        }catch (IOException e) {
            Log.e("Exception", "Something happened here");
            e.printStackTrace();
        }

        return null;
    }

但仍然没有任何反应。有什么建议吗?

像这样从资产中读取文件

  try {
            InputStream is = context.getAssets().open("file name");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

然后创建新文件并将此 InputStream 写入该文件。

在清单文件中授予读写权限。

像这样将输入流写入文件 ------

 try {
                InputStream inputStream = null;
                OutputStream output = null;
                try {
                    inputStream = getContentResolver().openInputStream(data.getData());
                    File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                            "picture_G.jpg");
                    output = new FileOutputStream(file);

                    byte[] buffer = new byte[4 * 1024]; // or other buffer size
                    int read;

                    while ((read = inputStream.read(buffer)) != -1) {
                        output.write(buffer, 0, read);
                    }
                    output.flush();


                } finally {
                    inputStream.close();
                    output.close();
                }
            } catch (Exception e) {
                e.printStackTrace(); // handle exception, define IOException and others
            }

在这种情况下,您还可以按如下方式在本地应用程序文件空间中创建和写入文件(改编自之前的回答):

private File createFileFromInputStream(InputStream inputStream) {
    try{
        File f = new File(getFilesDir(), "temp1.txt");
        OutputStream outputStream = new FileOutputStream(f);
        byte buffer[] = new byte[1024];
        int length = 0;

        while((length=inputStream.read(buffer)) > 0) {
            outputStream.write(buffer,0,length);
        }

        outputStream.close();
        inputStream.close();

        return f;
    } 
    catch (IOException e) { e.printStackTrace(); }
    return null;
}