Construct a java.io.File that reads from a byte array

后端 未结 4 667
失恋的感觉
失恋的感觉 2021-01-20 05:49

I\'m using an API that requires a java.io.File as input, but I\'d like to just send the API a byte array. Is there a way to create a File object that will read from my byte

4条回答
  •  既然无缘
    2021-01-20 06:14

    String strFilePath = "filePath";
    byte[] writeBytes = ...;
    try {
      FileOutputStream fos = new FileOutputStream(strFilePath);
      fos.write(byteArray);
      fos.close();
    } catch(FileNotFoundException ex) {
      // handle FileNotFoundException here
    } catch(IOException ioe) {
      // handle IOException here
    }
    

    EDIT: Usually I am not a fan of just giving code, but this whole streaming stuff used to confuse me quite a bit, so here is a solution ;).

    EDIT2: Ok, I just realized, that this is exactly, what the OP did NOT want to do. So this answer is not really useful...

提交回复
热议问题