IO流

IO流

一、什么是流

流:对输入和输出设备的抽象,流的本质就是数据的传递

根据数据流向,分为:
1.输入流:从输入流中读取数据,比如:键盘输入
2.输出流:向输出流中,写入数据,比如:打印机
根据处理数据的单位,分为:
1.字节流,以字节(Byte)为单位,处理任意类型的数据(图片,视频,文件等)
2.字符流,以字符(Char)为单位,对纯文本进行处理,其实就是字节流获取文本数据之后,没有直接处理,而是查找编码获取对应的文字进行!
根据功能,分为:
1.字点流:针对特定的IO设备,进行读写,又叫低级流
2.处理流:对已存在的流进行封装或者连接,来实现读写操作,又叫高级流!

1.字符输出流

		//1.设置路径
		String path="./src/com/main/test.txt";
		//true 拼接,接着上次的内容,继续写
		FileWriter fileWriter=null;
		try {
			fileWriter=new FileWriter(path, false);
			fileWriter.write("hello word");
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
		}

2.字符输入流

try {
			FileReader fileReader=new FileReader(path);
			//1.读取到的数据是整型,是一个字符对应的ASCII值,也就是Unicode编码值(默认十进制)
			int content=0;
			//2.read一次只能读取一个字符!并且任何字符的值不会为负数,因为编码值从0开始!
			while((content = fileReader.read())!=-1) {
				//System.out.println(content+" "+(char)content);
				System.out.print((char)content);
			}
			
		} catch (Exception e) {
			
		}

3.字节输出流

String filePath="./src/com/main/game.txt";
		try {
			//OutputStream 抽象父类,要通过子类new对象
			OutputStream outputStream=new FileOutputStream(filePath, false);
			String text="三生三世枕上书";
			outputStream.write(text.getBytes());
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

4.字节输入流

try {
			InputStream inputStream = new FileInputStream(filePath);
			//注意!字节流  读取的数据也是字节 . 并且读取到的仅仅是第一个字节中的内容
			int content = 0;
			//注意!此写法 对中文不够完善  因为中文占两个字节. 但是read()只能一个一个字节的读取!
			/*
			 * inputStream.read()  返回值为整型,是读到的字节
			while ((content = inputStream.read()) != -1) {
				System.out.println((char)content);
			}*/
			//优化后的方式:
			byte[] bytes = new byte[100];
			//length 存储要读取数据的字节数
			int length = 0;
			//inputStream.read(bytes) 返回值也是整型,是读取的长度
			while((length = inputStream.read(bytes)) !=  -1) {
				String str = new String(bytes, 0, length);
				System.out.println(str);
				
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

二、转换流

package com.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TranstionIO {

	public static void main(String[] args) {
		//转换流!!!
		//1.
		String path="src/com/mainhero.txt";
		//输出流
		try {
			FileOutputStream fileOutputStream=new FileOutputStream(path);
			OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream);
			BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
			bufferedWriter.write("鲁班大师 虞姬  狄仁杰");
			bufferedWriter.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//-------------------
		//输入流
		try {
			FileInputStream fileInputStream=new FileInputStream(path);
			InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
			BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
			//bufferedReader.readLine();
			String line="";
			String totalStr="";
			while((line=bufferedReader.readLine())!=null) {
				totalStr+=line;
			}
			System.out.println(totalStr);
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

三、拷贝图片

方法一:不带有缓冲区的
方法二:带有缓冲区的
方法三:使用系统缓冲区的

package com.main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyImage {

	public static void main(String[] args) {
		//路径
		String fromPath="C:\\Users\\小江\\Desktop\\家具\\7.png";
		String toPath="src/com/main/7.png";
		//调用方法1
		long start=System.currentTimeMillis();
		copyImage(fromPath, toPath);
		long end=System.currentTimeMillis();
		System.out.println("耗时(毫秒)"+(end-start));
		
		//调用方法2
		String toPath2="src/com/main/小k.png";
		long s1=System.currentTimeMillis();
		copyImageWithBuffer(fromPath, toPath2);
		long s2=System.currentTimeMillis();
		System.out.println("耗时(毫秒)"+(s2-s1));
		
		/*注意:缓冲流自带缓冲区域
		 * 缓冲字节流:
		BufferedInputStream
		BufferedOutputStream
		缓冲字符流:
		BufferedWriter
		BufferedRead
		*/
		//调用方法3
		String toPath3="src/com/main/小1.png";
		long s3=System.currentTimeMillis();
		copyImageWithSystemBuffer(fromPath, toPath3);
		long s4=System.currentTimeMillis();
		System.out.println("耗时(毫秒)"+(s4-s3));
		
	}
	//1.封装拷贝图片的方法
	public static void copyImage(String fromPath, String toPath) {
		try {
			//读图片
			FileInputStream fileInputStream=new FileInputStream(fromPath);
			//写入图片
			FileOutputStream fileOutputStream=new FileOutputStream(toPath);
			int countent=0;
			while((countent=fileInputStream.read())!=-1) {
				//读取一个字节以后 立马写入指定的目的路径中
				fileOutputStream.write(countent);
			}
			fileInputStream.close();
			fileOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//2拷贝图片(带缓冲区)
	public static void copyImageWithBuffer(String fromPath,String toPath) {
		
		try {
			//读图片
			FileInputStream fileInputStream=new FileInputStream(fromPath);
			//写图片
			FileOutputStream fileOutputStream=new FileOutputStream(toPath);
			//缓冲区:读取多少个字节以后,写入一次,提高读写效率
			//注意:缓冲区大小最好为1024
			byte[] bytes=new byte[1024];
			
			while((fileInputStream.read(bytes))!=-1) {
				//读取一个字节以后 立马写入指定的目的路径中
				fileOutputStream.write(bytes);
			}
			fileInputStream.close();
			fileOutputStream.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	//3.bufferedInputStream 和bufferedOutputStream 缓冲流方式
	public static void copyImageWithSystemBuffer(String fromPath, String toPath) {
		
		FileInputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		try {
			fileInputStream=new FileInputStream(fromPath);
			fileOutputStream=new FileOutputStream(toPath);
		} catch (Exception e) {
			
		}
		
		BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
		BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
		
		//开启边读边写
		int content = 0;
		try {
			while ((content=bufferedInputStream.read())!=-1) {
				bufferedOutputStream.write(content);
				
			}
			//注意:只需要关闭缓冲区流即可!因为缓冲流是基于普通字节流创建的!
//			fileInputStream.close();
//			fileOutputStream.close();
			bufferedInputStream.close();
			bufferedOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}

四、把对象写入文件中

例如:把学生对象(Student)中的数据写入 student.txt 中
1. 首先创建Student类学生类一定要实现Serializable接口,不然就会报一个NotSerializableException异常

package com.object;

import java.io.Serializable;

public class Student implements Serializable {
	private String name;
	private String sex;
	private int age;
	private String telNumber;
	public Student() {
		
	}
	
	public Student(String name, String sex, int age, String telNumber) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.telNumber = telNumber;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getTelNumber() {
		return telNumber;
	}
	public void setTelNumber(String telNumber) {
		this.telNumber = telNumber;
	}
	
	
	
}

2. 测试方法Main类

package com.object;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Main {

	public static void main(String[] args) {
		Student stu1=new Student("小王", "男", 18, "128374164");
		Student stu2=new Student("小件", "男", 18, "128374164");
		
		ArrayList<Student> arrayStu=new ArrayList<>();
		arrayStu.add(stu1);
		arrayStu.add(stu2);
		//1.准备文件路径
		String path="src/com/object/student.txt";
		//2.ObjectOutputStream 对象处理流
		//序列化操作 对象-->流
		FileOutputStream fileOutputStream=null;
		try {
			fileOutputStream=new FileOutputStream(path);
			ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
			objectOutputStream.writeObject(stu1);
			objectOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//3.读取对象
		//反序列化操作 流-->对象
		try {
			FileInputStream fileInputStream=new FileInputStream(path);
			ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
			Object obj =objectInputStream.readObject();
			//安全判断 instanceof 比较对象类型的关键字
			if(obj instanceof Student) {
				System.out.println("是student对象");
				Student stu=(Student)obj;
				System.out.println(stu);
			}else {
				System.out.println("不是student对象");
			}
			objectInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值