IO流
流的分类
按方向【重点】
- 输入流(Input)
- 输出流(Output)
按单位
-
字节流
操作的是byte
-
字符流
操作的是char
按功能
- 节点流
- 缓冲流
流的快速入门
1. 流的打开与关闭
package com.qf.pro2103.day20.stram;
import java.io.*;
import java.util.zip.InflaterInputStream;
public class FileIO {
public static void main(String[] args) {
try {
//输出一张图片
InputStream is = new FileInputStream("E:");
//输入一张图片
OutputStream os = new FileOutputStream("E:");
byte[] buff = new byte[521];
int len=0;
while(true){
//读到多少个字节
//读到了多少个,存到len变量里
//读,一次读512byte
len=is.read(buff);
//如果read的返回值为-1,代表读到了文件为,已经没有文件能读了
if(len==-1){
break;
}
//写,一次写512byte
os.write(buff,0,len);
}
//释放资源
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
流的读与写
byte[] buff = new byte[512];
int len = 0;
while (true) {
// 读到的字节,都在buff里
// 读到了多少个,存到len变量里
// 读,一次读512byte
len = is.read(buff);
// 如果read的返回值为-1,代表读到了文件尾了,已经没有任何东西能读了
if (len == -1) {
break;
}
// 写,一次写len 个 byte
os.write(buff, 0, len);
}
字节缓冲流
用于提升对系统磁盘io的使用效率
使用字节缓冲流,复制粘贴一张图片
创建缓冲流对象
package com.qf.pro2103.day20.FX;
import java.io.*;
public class IOO{
public static void main(String[] args) {
try{
InputStream is = new FileInputStream("E:");
OutputStream os = new FileOutputStream("E:");
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] b = new byte[512];
int i = 0;
while(true){
i=bis.read(b);
if(i==-1){
break;
}
bos.write(b,0,i);
}
bis.close();
bos.close();
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象字节流
创建User类
package com.qf.pro2103.day20.FX;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
private String moild;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMoild() {
return moild;
}
public void setMoild(String moild) {
this.moild = moild;
}
}
对象输出
将一个对象,存到磁盘中
package com.qf.pro2103.day20.FX;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class IOOout {
public static void main(String[] args) {
User user = new User();
user.setUsername("张三");
user.setPassword("123456000");
user.setMoild("131331313131");
try{
OutputStream os = new FileOutputStream("E:");
ObjectOutputStream OOS = new ObjectOutputStream(os);
OOS.writeUnshared(user);
os.close();
OOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象输入
将磁盘中的文件,读到对象中
package com.qf.pro2103.day20.FX;
import java.io.*;
public class IOOinput {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("E:");
ObjectInputStream ois = new ObjectInputStream(is);
Object s = ois.readObject();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
乱码
编码集
CharacterEncoding
字符流
字符输出流
package com.qf.pro2103.day20.FX;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IOwrite {
public static void main(String[] args) {
try {
Writer w = new FileWriter("E:");
String n ="sdjfhkja";
w.write(n);
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流
package com.qf.pro2103.day20.FX;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class IOread {
public static void main(String[] args) {
try {
Reader r = new FileReader("E:");
StringBuilder sb = new StringBuilder();
char[] buff = new char[512];
int i=0;
while (true){
i=r.read(buff);
if(i==-1){
break;
}
sb.append(buff,0,i);
}
System.out.println(sb);
r.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
-
InputStream
-
OutputStream
-
FileInputStream
-
FileOutputStream
-
BufferedInputStream
-
BufferedOutputStream
-
ObjectInputStream
-
ObjectOutputStream
-
Serializeble
-
Reader
-
Writer
-
FileReader
-
FileWriter