/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null //新建一个输出流
FileOutputStream fos = null //w文件包装输出流
File file = null
try {
File dir = new File(filePath)
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs()
}
file = new File(filePath+"\\"+fileName) //新建一个file类
fos = new FileOutputStream(file)
bos = new BufferedOutputStream(fos) //输出的byte文件
bos.write(bfile)
} catch (Exception e) {
e.printStackTrace()
} finally {
if (bos != null) {
try {
bos.close() //关闭资源
} catch (IOException e1) {
e1.printStackTrace()
}
}
if (fos != null) {
try {
fos.close() //关闭资源
} catch (IOException e1) {
e1.printStackTrace()
} 更多技术问题可在itjob技术群交流
}
}
}
ObjectOutputStream oos = null//对象输出流ByteArrayOutputStream baos = null//byte数组输出流
ByteArrayInputStream bais = null//对象输入流
try {
//序列化
baos = new ByteArrayOutputStream()
oos = new ObjectOutputStream(baos)//将数组流传入对象流
oos.writeObject(new Integer(1))//用对象流读取对象。
byte[] bytes = baos.toByteArray()//用数组流将传入的对象转化为byte数组
//反序列化
bais = new ByteArrayInputStream(bytes)
ObjectInputStream ois = new ObjectInputStream(bais)
Integer i = (Integer)ois.readObject()
System.out.println(i)
} catch (Exception e) {
}以上是把integer类型转化成byte[]数组类型。注:基本类型要转化为byte[]数组的话,需要用该基本类型的引用类。比如int的引用类是integer,就行了所有的类型,包括class都可以用这种序列化方式来转成byte[],