IO笔记
IO流
- 输入输出流
- 在Java API中,可以从其中读入一个字节序列的对象称作输入流,而可以向其中写入一个字节序列的对象称作输出流。
- 这些字节序列的来源地和目的地通常是文件,但是也可以是网络连接,甚至是内存块
- 字节流/字符流
- 将输入输出流的成员按照它们的使用方法来进行划分,可分为字节流和字符流
- 读写单个字节或字节数组使用字节流
- InputStream
- OutputStream
- 对于Unicode文本可以使用字符流
- Reader
- Writer
IO流组织结构图
节点流处理流一览
字节流
- 常用子类
- FileInputStream -> 字节输入流
/**
* 字节输入流
*/
public class test {
public static void main(String[] args) throws IOException {
int readinfo;
String filePath = "/Users/zhangxuyang/Desktop/hello.txt";
FileInputStream inputStream = new FileInputStream(filePath);
while ((readinfo = inputStream.read()) != -1){
System.out.print((char) readinfo);
}
inputStream.close();
}
}
//console.log = Maybe I can't decide my life, but I can face life calmly.
- FileOutputStream -> 字节输出流
public class test {
public static void main(String[] args) throws IOException {
int readinfo;
String filePath = "/Users/zhangxuyang/Desktop/hello.txt";
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] bytes = "Maybe I can't decide my life, but I can face life calmly.".getBytes();
for (byte b : bytes){
outputStream.write(b);
}
outputStream.close();
}
}
- 字节输入输出流 -> 文件拷贝
public class test {
public static void main(String[] args) throws IOException {
int readsize;
String filePath = "/Users/zhangxuyang/Desktop/hello.txt";
String filePath2 = "/Users/zhangxuyang/Desktop/hello3.txt";
FileInputStream inputStream = new FileInputStream(filePath);
FileOutputStream outputStream = new FileOutputStream(filePath2);
byte[] buf = new byte[1024];
while ((readsize = inputStream.read(buf)) != -1){
outputStream.write(buf,0,readsize);
}
if(inputStream!=null){
inputStream.close();
}
if(outputStream!=null){
outputStream.close();
}
}
}
- BufferedInputStream/BufferedOutputStream -> 缓冲流 (装饰者模式)
/**
* 拷贝文件
*/
public class test {
public static void main(String[] args) throws IOException {
int size;
String FilePath = "/Users/zhangxuyang/Desktop/hello.txt";
String FilePath2 = "/Users/zhangxuyang/Desktop/hello3.txt";
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(FilePath));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(FilePath2));
byte[] bytes = new byte[1024];
while ((size = inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,size);
}
inputStream.close();
outputStream.close();
}
}
- ObjectOutputStream -> 序列化对象
/**
* 对象序列化
*/
public class test {
public static void main(String[] args) throws IOException {
//序列化文件后缀名可以任意
String filepath = "/Users/zhangxuyang/Desktop/hello.ser";
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(filepath));
stream.writeObject(new Dog(10,"旺财"));
stream.close();
System.out.println("完成对象序列化");
}
}
//只有实现了Serializable的类才可以被序列化
public class Dog implements Serializable {
//提高版本兼容性
private static final long serialVersionUID = 1L;
public Dog(int age,String name){
this.age = age;
this.name = name;
}
//被static或transient修饰的属性,无法被序列化
private int age;
private String name;
@Override
public String toString() {
return "Dog{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
- ObjectOutputStream -> 对象反序列化
/**
* 对象反序列化
*/
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//序列化文件后缀名可以任意
String filepath = "/Users/zhangxuyang/Desktop/hello.ser";
ObjectInputStream stream = new ObjectInputStream(new FileInputStream(filepath));
//要按照序列化时的顺序读取对象
Dog dog = (Dog) stream.readObject();
System.out.println(dog);
System.out.println("完成对象反序列化");
}
}
//console.log = Dog{age=10, name='旺财'}
字符流
- 常用子类
- FileReader -> 字符输入流
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
int size;
String filepath = "/Users/zhangxuyang/Desktop/hello2.txt";
FileReader reader = new FileReader(filepath);
char[] buf = new char[8];
while ((size=reader.read(buf))!=-1){
System.out.print(new String(buf,0,size));
}
reader.close();
}
}
//console.log = 事实上我已经无法决定自己的人生,但也能够坦然面对生活。
- FileWriter -> 字符输出流
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filepath = "/Users/zhangxuyang/Desktop/hello2.txt";
FileWriter writer = new FileWriter(filepath);
writer.write("事实上我已经无法决定自己的人生,但也能够坦然面对生活。");
writer.close();
}
}
- Buffered拷贝文件
//注意:不要使用字符流去操作二进制文件!
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String line;
String FilePath = "/Users/zhangxuyang/Desktop/hello2.txt";
String FilePath2 = "/Users/zhangxuyang/Desktop/hello3.txt";
BufferedReader reader = new BufferedReader(new FileReader(FilePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath2));
while ((line = reader.readLine())!=null){
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
}
}
补充
- 标准输入输出流 -> 输入设备/输出设备
- 转换流 -> 处理乱码
- Properties -> 操作配置文件
- 标准输入输出流
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//in is System类的 public final static InputStream in = null;
//System.in 编译类型 InputStream
//System.in 运行类型 BufferedInputStream
//表示的是标准输入 键盘
System.out.println(System.in.getClass());
//in is public final static PrintStream out = null;
//编译类型 PrintStream
//运行类型 PrintStream
//表示标准输出 显示器
System.out.println(System.out.getClass());
}
}
- 转换流
//输入流转换
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filepath = "/Users/zhangxuyang/Desktop/hello2.txt";
int size;
char[] chars = new char[8];
//将字节流转换为字符流时指定编码
InputStreamReader reader = new InputStreamReader(new FileInputStream(filepath),"UTF-8");
while ((size = reader.read(chars))!=-1){
System.out.print(new String(chars,0,size));
}
reader.close();
}
}
//输出流转换
public class test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filepath = "/Users/zhangxuyang/Desktop/hello2.txt";
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filepath),"UTF-8");
writer.write("事实上我已经无法决定自己的人生,但也能够坦然面对生活。");
writer.close();
}
}
- Properties
//读取配置文件并获取对应的值
public class test {
public static void main(String[] args) throws IOException {
String filepath = "src/cs.properties";
Properties properties = new Properties();
properties.load(new FileReader(filepath));
//输出所有配置信息
properties.list(System.out);
System.out.println(properties.getProperty("name"));
}
}
//创建配置文件并修改内容
public class test {
public static void main(String[] args) throws IOException {
String filepath = "src/cs.properties";
Properties properties = new Properties();
properties.setProperty("name","小猫");
//setProperty可以添加或修改值
properties.setProperty("age","5");
properties.store(new FileOutputStream(filepath),null);
}
}
如果觉得有帮助,可转发让更多小伙伴看到。赏个赞吧~
END

浙公网安备 33010602011771号