存储字节数组

时间:2014-12-21 13:39:07

标签: java arrays

您好我使用以下代码将jpeg图像转换为字节数组。但我想将字节数组存储在某个磁盘上。我不知道怎么做。

public class ConvertImage {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        File file = new File("C:\\rose.jpg");

        FileInputStream fis = new FileInputStream(file);
        //create FileInputStream which obtains input bytes from a file in a file system
        //FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                //Writes to this byte array output stream
                bos.write(buf, 0, readNum); 
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
        }

        byte[] bytes = bos.toByteArray();  
     }
}

0 个答案:

没有答案