Java: convert a file to a byte array, then convert byte array to a file.
2012-03-07 16:32
597 查看
Java: convert a file to a byte array, then convert byte array to a file.
If you need Java code to convert a file to a byte array and then convert it back, this will work for you!
First, to convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().
Second, to convert byte array back to the original file, FileOutputStream class is used. A file output stream is an output stream for writing data to a File or to a FileDescriptor.
Here is the code.
There are various applications of this conversion. For example, convert file into byte to save into database, transfer file to other system using web service, etc.
Similarly, you can convert an image to byte array, see this post.
References:
1. ByteArrayOutputStream Java Doc
2. FileOutputStream Java Doc
Java: convert a file to a byte array, then convert byte array to a file.
Under Category: OthersIf you need Java code to convert a file to a byte array and then convert it back, this will work for you!
First, to convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().
Second, to convert byte array back to the original file, FileOutputStream class is used. A file output stream is an output stream for writing data to a File or to a FileDescriptor.
Here is the code.
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class genFile { public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("java.pdf"); FileInputStream fis = new FileInputStream(file); //System.out.println(file.exists() + "!!"); //InputStream in = resource.openStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); //no doubt here is 0 //Writes len bytes from the specified byte array starting at offset off to this byte array output stream. System.out.println("read " + readNum + " bytes,"); } } catch (IOException ex) { Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex); } byte[] bytes = bos.toByteArray(); //below is the different part File someFile = new File("java2.pdf"); FileOutputStream fos = new FileOutputStream(someFile); fos.write(bytes); fos.flush(); fos.close(); } }Why would someone like to convert a file to byte array?
There are various applications of this conversion. For example, convert file into byte to save into database, transfer file to other system using web service, etc.
Similarly, you can convert an image to byte array, see this post.
References:
1. ByteArrayOutputStream Java Doc
2. FileOutputStream Java Doc
相关文章推荐
- Java基础---Java---IO流-----对象的序列化、管道流、RandomAccessFile、数据类型的流对象DataStream、ByteArrayStream
- Java: How to convert int[] to byte[]
- convert a byte array to a hexadecimal string
- org.apache.shiro.codec.CodecException: Unable to convert source [hello] to byte array using encoding
- [Leetcode] Convert Sorted Array to Binary Search Tree (Java)
- 【Java App Convert to EXE File】
- How to convert Java String into byte[]?
- Convert Sorted Array to Binary Search Tree leetcode java
- convert-sorted-array-to-binary-search-tree Java code
- Convert Document to ByteArrayInputStream
- Java基础---Java---IO流-----对象的序列化、管道流、RandomAccessFile、数据类型的流对象DataStream、ByteArrayStream
- 【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】
- LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解
- How to convert char array to String in Java
- PHP write byte array to file
- Convert byte array to char* in a clr wrapper
- 【Leetcode】Convert Sorted Array to Binary Search Tree in JAVA
- Java基础 管道流 RandomAccessFile DataStream ByteArrayStream 字符编码
- LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解
- struts2下载文件时出现:java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be cast to 错误