문제

I have an array of bytes

bytes[] doc

How can I create an instance of new File from those bytes?

도움이 되었습니까?

해결책

 try (FileOutputStream fileOuputStream = new FileOutputStream("filename")){
    fileOuputStream.write(byteArray);
 }

다른 팁

Try this:

Files.write(Paths.get("filename"), bytes);

A simple program that will write a byte[] into a file.

import java.io.File;
import java.io.FileOutputStream;

public class BytesToFile {

    public static void main(String[] args) {

        byte[] demBytes = null; // Instead of null, specify your bytes here.

        File outputFile = new File("LOCATION TO FILE");

        try ( FileOutputStream outputStream = new FileOutputStream(outputFile); ) {

            outputStream.write(demBytes);  // Write the bytes and you're done.

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Note: The try-catch statement requires a Java version >= 1.7. Remember to change the bytes from null to correspond to your byte array.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top