文件读取到字节数组
1.文件到程序:InputStream
2.程序到字节数组:ByteArrayOutputStream
本质:文件字节输入流+字节数组输出流
ByteArrayOutputStream在创建源时系统内部维护了,选择流时不需要关联源。在最后可以不需要释放,获取数据使用toByteArray。
package cn.rou.io;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 图片读取到字节数组
* 字节数组写出到文件
*
* @author : RouBao
*
*
*/
public class IOtest09 {
public static void main(String[] args) {
byte[] dates = fileToByteArray("DRB.jpg");
System.out.println(dates.length);
}
/*
* 1.文件读取到字节数组
* 1)、文件到程序 FileInputStream
* 2)、程序到字节数组 ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath) {
// 创建源
File file = new File(filePath);
ByteArrayOutputStream baos = null;
// 选择流
InputStream is = null;
baos = new ByteArrayOutputStream();
//操作
try {
is = new FileInputStream(file);
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放
try {
if (is != null) {
is.close();
}
} catch (Exception e2) {
}
}
return null;
}
}
字节数组写出到文件
将上述由文件转成的字节数组再次写出到文件(纯为练习,实际会直接传入一个字节数组)。
1.字节数组到程序:ByteArrayInputStream
2.程序到文件:OutputStream
本质:字节数组输入流+文件字节输出流
package cn.rou.io;
import java.io.ByteArrayInputStream;
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.io.InputStream;
import java.io.OutputStream;
/**
* 图片读取到字节数组
* 字节数组写出到文件
*
* @author : RouBao
*
*
*/
public class IOtest09 {
public static void main(String[] args) {
byte[] dates = fileToByteArray("DRB.jpg");
System.out.println(dates.length);
byteArrayToFile(dates,"BtF.jpg");
}
/*
* 1.文件读取到字节数组
* 1)、文件到程序 FileInputStream
* 2)、程序到字节数组 ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath) {
// 创建源
File file = new File(filePath);
ByteArrayOutputStream baos = null;
// 选择流
InputStream is = null;
baos = new ByteArrayOutputStream();
//操作
try {
is = new FileInputStream(file);
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放
try {
if (is != null) {
is.close();
}
} catch (Exception e2) {
}
}
return null;
}
/*
* 2.字节数组写出到文件
* 1)、字节数组到程序 ByteArrayInputStream
* 2)、程序到文件 OutpuStream
*/
public static void byteArrayToFile(byte[] by, String filePath) {
// 创建源
byte[] flush = by;
File file = new File(filePath);
// 选择流
ByteArrayInputStream bais = null;
OutputStream os = null;
// 操作
try {
bais = new ByteArrayInputStream(flush);
os = new FileOutputStream(file);
byte[] f = new byte[1024];
int len = -1;
while ((len = bais.read(f)) != -1) {
os.write(f, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放
try {
if (os != null) {
os.close();
}
} catch (Exception e2) {
}
}
}
}