Java 将字节数组转换为分块的文件

Java 将字节数组转换为分块的文件,java,android,file,bytearray,chunks,Java,Android,File,Bytearray,Chunks,我知道有一种方法可以将文件转换为块中的字节数组,下面是一个示例代码: InputStream inputStream = new FileInputStream(videoFile); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int bytesRead =0; while ((bytesRead = inputStream.re

我知道有一种方法可以将文件转换为块中的字节数组,下面是一个示例代码:

 InputStream inputStream = new FileInputStream(videoFile);
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     byte[] b = new byte[1024];
     int bytesRead =0;
     while ((bytesRead = inputStream.read(b)) != -1)
     {
       bos.write(b, 0, bytesRead);
     }
我正在寻找相反的方法:一种将字节数组转换成块文件的方法。我没有找到任何分块执行的示例。

字节[]到文件:

 FileOutputStream fop = null; File file;
        try {
            file = new File(filePath);
            fop = new FileOutputStream(file, true);
            fop.write(chunk);
            fop.flush();
            fop.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
请尝试将文件转换为字节[]:

 InputStream is = new FileInputStream(file);
        int length = (int) file.length();           
        int take = 262144;//size of your chunk
        byte[] bytes = new byte[take];
                    int offset=0;
        int a = 0;
        do {
            a = is.read(bytes, 0, take);
            offset += a;
            //And you can add here each chunk created in to a list, etc, etc.
            //encode to base 64 this is extra :)
            String str = Base64.encodeToString(bytes, Base64.DEFAULT);

        } while (offset < length);=
        is.close();
        is=null;
InputStream is=新文件InputStream(文件);
int length=(int)file.length();
int-take=262144//块的大小
byte[]bytes=新字节[take];
整数偏移=0;
int a=0;
做{
a=is.read(字节,0,取);
偏移量+=a;
//您可以在这里将创建的每个块添加到列表中,等等。
//编码到基64这是额外的:)
字符串str=Base64.encodeToString(字节,Base64.DEFAULT);
}而(偏移量<长度)=
is.close();
is=null;
字节[]到文件:

 FileOutputStream fop = null; File file;
        try {
            file = new File(filePath);
            fop = new FileOutputStream(file, true);
            fop.write(chunk);
            fop.flush();
            fop.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
请尝试将文件转换为字节[]:

 InputStream is = new FileInputStream(file);
        int length = (int) file.length();           
        int take = 262144;//size of your chunk
        byte[] bytes = new byte[take];
                    int offset=0;
        int a = 0;
        do {
            a = is.read(bytes, 0, take);
            offset += a;
            //And you can add here each chunk created in to a list, etc, etc.
            //encode to base 64 this is extra :)
            String str = Base64.encodeToString(bytes, Base64.DEFAULT);

        } while (offset < length);=
        is.close();
        is=null;
InputStream is=新文件InputStream(文件);
int length=(int)file.length();
int-take=262144//块的大小
byte[]bytes=新字节[take];
整数偏移=0;
int a=0;
做{
a=is.read(字节,0,取);
偏移量+=a;
//您可以在这里将创建的每个块添加到列表中,等等。
//编码到基64这是额外的:)
字符串str=Base64.encodeToString(字节,Base64.DEFAULT);
}而(偏移量<长度)=
is.close();
is=null;

您只需使用
FileOutputStream
类中的或方法。

您只需使用
FileOutputStream
类中的或方法。

考虑将问题泛化

此方法以块的形式复制数据:

  public static <T extends OutputStream> T copy(InputStream in, T out)
      throws IOException {
    byte[] buffer = new byte[1024];
    for (int r = in.read(buffer); r != -1; r = in.read(buffer)) {
      out.write(buffer, 0, r);
    }
    return out;
  }

考虑将问题概括化

此方法以块的形式复制数据:

  public static <T extends OutputStream> T copy(InputStream in, T out)
      throws IOException {
    byte[] buffer = new byte[1024];
    for (int r = in.read(buffer); r != -1; r = in.read(buffer)) {
      out.write(buffer, 0, r);
    }
    return out;
  }

offset
是值为
0
的int吗?它不是将文件转换为字节[]的代码吗?我正在寻找将字节[]转换为文件。很抱歉,错误是
offset
值为
0
的int?这不是将文件转换为字节[]的代码吗?我正在寻找将字节[]转换为文件。很抱歉出错