JavaIO流基础学习

JavaIO流基础学习

@author:HB、ocean

@time:2020-3-28

流的分类

  • 按单位
    1. 字节流:读写所有数据,以字节为单位
    2. 字符流 :读写中文,以字符为单位
  • 按功能
    1. 节点流(底层流):具有实际传输数据的读写功能
    2. 过滤流:在节点流之上的增强功能

1.字节流(可复制所有文件)

FileInputStream

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 创建字节流对象
	FileInputStream fileInputStream = new FileInputStream("File/hello.txt");
	
	// 读取文件
	/*int read = 0;
	while((read=fileInputStream.read())!=-1) {
		System.out.print((char)read);
	}*/
	// 缓冲区 UTF-8编码一个中文站3个字节
	byte[] buf = new byte[3];
	// 计数
	int count = 0;
	while((count=fileInputStream.read(buf))!=-1) {
		System.out.print(new String(buf,0,count));
	}
	
	// 关闭字节流
	fileInputStream.close();
	System.out.println("执行完毕");

}

FileOutputStream

public class IO_FileOutPutStream {
	public static void main(String[] args) throws IOException {
		// 创建字节输出流对象
		FileOutputStream fileOutputStream = new FileOutputStream("File/hello.txt");
		// 写入字节
		String write = "output:hello bh";
		fileOutputStream.write(write.getBytes());
		// 关闭字节输出流
		fileOutputStream.close();
		System.out.println("执行完毕");
	}

CopyFile

	public static void main(String[] args) throws IOException {
		// 读取文件
		File file = new File("File/git_pic.png");
		long start =System.currentTimeMillis();
		// 创建字节输入输出流
		FileInputStream fIS = new FileInputStream(file);
		FileOutputStream fOS = new FileOutputStream(file.getPath().substring(0, file.getPath().indexOf('.')-1)+"_copyfile.png");
		// 复制文件 无缓冲区 用时:900ms 读写频繁 读一个字节写一个字节
//		int count = 0;
//		while((count=fIS.read())!=-1) {
//			fOS.write(count);
//		}
		// 使用缓冲区 用时:1ms 减少IO 
		byte[] buf = new byte[1024];
		int count = 0;
		while((count=fIS.read(buf))!=-1) {
			fOS.write(buf,0,count);
		}
		// 关闭流
		fIS.close();
		fOS.close();
		long end =System.currentTimeMillis();
		System.out.println("复制完毕");
		System.out.println("用时:"+(end-start)+"ms");
	}

2.字符流

FileReader

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 字符流读取文件
	FileReader fileReader = new FileReader("File/hello.txt");
	// 创建缓冲区 读取文件
	// 缓冲区 
	char[] buf = new char[2];
	// 计数
	int count = 0;
	while((count=fileReader.read(buf))!=-1) {
		System.out.print(new String(buf,0,count));
	}
	
	// 关闭字节流
	fileReader.close();
	System.out.println("执行完毕");
	
}

FileWriter

public static void main(String[] args) throws IOException {
	// 创建字符输出流对象 true代表追加
	FileWriter fileWriter = new FileWriter("File/hello1.txt",true);
	
	// 写入字符
	String write = "你好 炳汉";
	for(int i=0;i<5;i++) {
		fileWriter.write(write+i+"\r");
	}
	// 关闭字符输出流
	fileWriter.close();
	System.out.println("执行完毕");
	
}

3.缓冲流

BufferedInputStream/BufferedOuputStream

  1. 提高IO效率,减少访问磁盘的次数
  2. 数据存储在缓冲区中,flush(冲洗)是将缓冲区的内容写入文件中,也可以直接close,close默认包含了flush。
public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 创建字节输入流
	FileInputStream fileInputStream = new FileInputStream("File/hello.txt");
	// 创建字节缓冲流
	BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
	// 读取字节
	// 计数
	int count=0;
	while((count=bufferedInputStream.read())!=-1) {
		System.out.print((char)count);
	}
	// 关闭缓冲流 包含关闭输入流
	bufferedInputStream.close();

}
public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 创建字节输出流
	FileOutputStream fileOutputStream = new FileOutputStream("File/hello.txt");
	// 创建字节缓冲流
	BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
	// 写入文件
	for(int i=0;i<5;i++) {
		bufferedOutputStream.write(("This is BufferedOutputStream:"+i+"\n").getBytes());//写入8k缓冲区
		bufferedOutputStream.flush();// 刷新到硬盘
	}
	// 关闭缓冲流 包含关闭输入流
	bufferedOutputStream.close();
	System.out.println("执行完毕");

}

BufferedReader/BufferedWriter

  1. 高效读写
  2. 支持输入换行符
  3. 可一次写一行、读一行
    • readLine() 读一行
    • newLine() 写入一个行分割符(兼容性更好)
public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 字符流读取文件
	FileReader fileReader = new FileReader("File/hello1.txt");
	BufferedReader bufferedReader = new BufferedReader(fileReader);
	// 读取文件
	// 计数
	// 直接读
	/*int count = 0;
	while((count=bufferedReader.read())!=-1) {
		System.out.print((char)count);
	}*/
	// 按行读!
	String line = "";	
	while((line=bufferedReader.readLine())!=null) {
		System.out.println(line);
	}
	
	// 关闭字节流
	bufferedReader.close();
	
}
public static void main(String[] args) throws IOException {
	// 创建字符输出流对象 true代表追加
	FileWriter fileWriter = new FileWriter("File/hello1.txt",true);
	BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
	
	// 写入字符
	String write = "你好 炳汉";
	for(int i=0;i<5;i++) {
		bufferedWriter.write(write+i);
		bufferedWriter.newLine();// 换行 windows/r/n Linux Mac /n
	}
	// 关闭字符输出流
	bufferedWriter.close();
	System.out.println("执行完毕");
	
}

4.对象流(对象序列化)

注:

  1. 序列化类必须实现Serializable(串行化的)接口
  2. 序列化类中的对象属性要求实现Serializable(串行化的)接口
  3. 序列化版本号ID,保证是同一个类 serialVersionUID
  4. 使用transient(瞬间的)修饰属性,不能被序列化
  5. 静态属性不能被序列化
  6. 序列化多个对象,可借助集合实现

对象类

public class Student implements Serializable{
	private String name;
	private int age;
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	

}

ObjectInputStream

public static void main(String[] args) throws IOException, ClassNotFoundException {
	// 创建底层流
	FileInputStream fileInputStream = new FileInputStream("File/Student.bin");
	// 创建对象流
	ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
	// 读取文件("反序列化") 读一次读完
	Object student = (Student)objectInputStream.readObject();
	System.out.println(student.toString());
	// 关闭字节输出流
	objectInputStream.close();
	System.out.println("执行完毕");
}

ObjectOutputStream

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 创建底层流 
	FileOutputStream fileOutputStream = new FileOutputStream("File/Student.bin");
	// 创建对象流
	ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
	//序列化 写入操作
	Student lifour =new Student("李四",24);
	objectOutputStream.writeObject(lifour);
	// 关闭流
	objectOutputStream.close();
	System.out.println("序列化完毕");

}

5.打印流

  1. 封装了print()、println()方法,支持写入后换行
  2. 支持数据原样打印(支持多种数据类型)

PrintWriter

public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	// 创建打印流
	PrintWriter printWriter = new PrintWriter("File/print.txt");
	// 打印
	printWriter.print("start:");
	printWriter.print("我是李炳汉");
	printWriter.println(97);
	printWriter.println(3.14159265358979323846);
	printWriter.println(new Object());
	printWriter.append("This is append");
	// append可以追加null
	printWriter.append(null);
	// 关闭 
	printWriter.close();
	System.out.println("执行完毕");
}

6.转换流

1.字节流向字符流的转换

设置字符的编码方式

InputStreamReader

public static void main(String[] args) throws IOException {
	// 创建字节流、转换流 编码utf-8 GBK编码就是ANSI
	FileInputStream fileInputStream = new FileInputStream("File/print.txt");
	InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
	// 读取文件
	int count = 0;
	while((count=inputStreamReader.read())!=-1) {
		System.out.print((char)count);
	}
	// 关闭流 关闭过滤流即可关闭底层流
	inputStreamReader.close();
}

OutputStreamWriter

public static void main(String[] args) throws IOException {
	// 创建字节流、转换流 编码utf-8 GBK编码就是ANSI
		FileOutputStream fileOutputStream = new FileOutputStream("File/print1.txt");
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8");
	// 写入文件
		for(int i=0;i<5;i++) {
			outputStreamWriter.write("开始:This is num:"+i+"\n");
		}
	
	// 关闭流 关闭过滤流即可关闭底层流
	outputStreamWriter.close();
	System.out.println("执行完毕");
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值