IO流
流的本质是数据传输,根据数据传输特性将流抽象为各种类。
根据处理数据类型分为:字符流和字节流
根据数据流向分为:输入流和输出流
字符流的由来:因为数据编码的不同,而有了对字符进行高效操作的流对象,本质基于字节流。
区别
字节流以字节(8bit)为单位;字符流以字符为单位。
字节流能处理所有类型的数据(如图片、avi等);而字符流只能处理字符类型的数据。
字节流在操作的时不会用到缓冲区,是文件本身的直接操作;字符流在操作时会用到缓冲区的,通过缓冲区来操作文件。
字节流读入实例
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,
它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。
public static void main(String args[]) throws Exception{
// 第1步、使用 File 获取目标文件
File f= new File("d:" +File.separator + "hello.txt");
// 第2步、创建InputStream 并通过子类实例化
InputStream input = null;
input = new FileInputStream(f);
// 第3步、进行读操作
byte b[] = new byte[(int)f.length()]; // 创建容器
int len = input.read(b); // 读取内容到容器
// 第4步、关闭输出流
input.close();
System.out.println("读入数据的长度:" + len);
System.out.println("内容为:" + new String(b)); // 把 byte 数组变为字符串输出
}
字节流写出实例
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
public static void main(String args[]) throws Exception{
// 第1步、使用 File 获取要写入的文件
File f= new File("d:" + File.separator + "test.txt");
// 第2步、创建 OutputStream 并通过子类实例化
OutputStream out = null; // 准备好一个输出的对象
out = new FileOutputStream(f,true); // 此处表示在文件末尾追加内容
// 第3步、进行写操作
String str = "Hello World!!!"; // 准备一个字符串
byte b[] = str.getBytes(); // 只能写入byte数组,所以将字符串变为byte数组
for(int i=0;i<b.length;i++){
out.write(b[i]);
}
// 第4步、关闭输出流
out.close();
}
字符流读取
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。
public static void main(String args[]) throws Exception{ //异常抛出,不处理
// 第1步、使用 File 获取要写入的文件
File f= new File("d:" + File.separator + "hello.txt");
// 第2步、创建 Reader 并通过子类实例化
Reader input = null;
input = new FileReader(f);
// 第3步、进行读操作
char c[] = new char[1024]; // 所有的内容都读到此数组之中
int temp = 0;
int len = 0;
//逐个读取
while((temp=input.read())!=-1){
// 如果不是-1就表示还有内容,可以继续读取
c[len] = (char)temp;
len++;
}
// 第4步、关闭输出流
input.close();
System.out.println("内容为: " + new String(c,0,len)); // 把字符数组变为字符串输出
}
字符流写出
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
public static void main(String args[]) throws Exception{ // 异常抛出,不处理。
// 第1步、用 File 获取要写入对象
File f= new File("d:" + File.separator + "test2.txt"); // 声明File对象
// 第2步、创建 Writer 通过子类实例化
Writer out = null;
out = new FileWriter(f);
// 第3步、进行写操作
String str = "Hello World!!!";
out.write(str);
// 第4步、关闭输出流
out.close();
}
图片操作
static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
//将字符串写到本地Txt文件中
private static void saveAsFileOutputStream(String content) {
String file = "E:/TestImg/02/TestStringStream.txt";
FileOutputStream foutput = null;
try {
foutput = new FileOutputStream(file);
// foutput.write(content.getBytes("UTF-8"));
foutput.write(content.getBytes());
} catch(IOException ex) {
ex.printStackTrace();
} finally {
try {
foutput.flush();
foutput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("文件保存成功。" + file);
}
//将图片转为二进制字符串
public static String getImageBinary(){
File f = new File("E:/TestImg/01/B2.jpg"); //这里gif动态图不可以,虽然在后面也能输出gif格式,但是却不是动图
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//将字符串转成图片
public static void base64StringToImage(String base64String) throws IOException {
ByteArrayInputStream bais = null;
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
File w2 = new File("E:/TestImg/02/B2.jpg");//可以是jpg,png格式
ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动
} catch (IOException e) {
e.printStackTrace();
}finally {
bais.close();
}
}