본문 바로가기
Java

Java Byte[] TO File (바이트배열에서 파일로 저장)

by 전재훈 2019. 3. 18.
반응형

바이트 배열을 파일로 저장해야하는 상황이 있습니다.

저는 데이터베이스에 있는 blob 이미지를 파일로 저장할때 사용했습니다.

그외에 여러 곳에서 활용할 수 있는데 이럴때는 아래 함수를 만든 후 사용하면 됩니다.


public void writeToFile(String filename, byte[] pData)

{

    if(pData == null){

        return;

    }


    int lByteArraySize = pData.length;

    System.out.println(filename);


    try{

        File lOutFile = new File("C:/Users/madev/Downloads/test/"+filename);

        FileOutputStream lFileOutputStream = new FileOutputStream(lOutFile);

        lFileOutputStream.write(pData);

        lFileOutputStream.close();

    }catch(Throwable e){

        e.printStackTrace(System.out);

    }

}

반응형

댓글