Java中的File与IO流(2)

缓冲流

1.字节缓冲流

​BufferedInputStream: 该类实现了输入流的缓冲,为其提供一个内部的缓冲区数组.当我们读取字节时,内部缓冲区将根据所需要读取数据自动填充,一次可以读取多个字节

​BufferedOutputStream: 该类实现缓冲输出流.通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入字节的每个字节导致底层系统的频繁调用.

构造方法
方法名描述
BufferedInputStream(in)创建字节缓冲输入流对象并实现字节输入流
BufferedOutputStream(out)创建字节缓冲输出流对象
public class CopyAviDemo {

	public static void main(String[] args) {
		// 记录开始时间
		long start = System.currentTimeMillis();
		try {
			// 一次读取一个字节
			// method1();
			// method2();

			// 一次读取一个字节数组
			// method3();
			method4();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 记录结束时间
		long end = System.currentTimeMillis();
		System.out.println("复制所用时间为:" + (end - start));
	}

	private static void method4() {
		// 在JDK1.7之后,可以将流放入try后面的括号中,使用完成后自动关闭
		try (InputStream in = new FileInputStream("E:\\实现购物商城菜单.mp4");
				OutputStream out = new FileOutputStream("E:\\Java\\3.cai.mp4");) {
			// 设置缓存空间
			byte[] bs = new byte[1024];
			// 设置读取长度
			int len;
			while ((len = in.read(bs)) != -1) {
				// 将读取的内容写出到指定文件
				out.write(bs, 0, len);
				out.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void method3() throws Exception {
		// 创建缓冲流
		BufferedInputStream bin = new BufferedInputStream(
				new FileInputStream("E:\\实现购物商城菜单.mp4"));
		BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\\Java\\3.cai.mp4"));
		// 创建外部自定义缓冲数组
		byte[] bs = new byte[1024];
		int len;
		while ((len = bin.read(bs)) != -1) {
			bout.write(bs, 0, len);
		}
		bin.close();
		bout.close();
	}

	// 基本字节流单个字节读取文件
	private static void method2() {
		// 在JDK1.7之后,可以将流放入try后面的括号中,使用完成后自动关闭
		try (InputStream in = new FileInputStream("E:\\实现购物商城菜单.mp4");
				OutputStream out = new FileOutputStream("E:\\Java\\3.cai.mp4");) {
			// 设置读取长度
			int by;
			while ((by = in.read()) != -1) {
				// 将读取的内容写出到指定文件
				out.write(by);
			}
			System.out.println("复制成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 字节缓冲流每次读取一个字节
	private static void method1() throws Exception {
		// 创建缓冲流
		BufferedInputStream bin = new BufferedInputStream(
				new FileInputStream("E:\\实现购物商城菜单.mp4"));
		BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\\Java\\3.cai.mp4"));
		int by;
		while ((by = bin.read()) != -1) {
			bout.write(by);
		}
		bin.close();
		bout.close();
	}

}
2.字符缓冲流

​BufferedWriter: 将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写出,可以指定缓冲区大小,或者可以使用默认大小,可以用于大多数,字符串的写出

​BufferedReader: 从字符输出流中读取文本,缓冲字符,提供字符,数组和字符串行的高效读取,可以指定缓冲区大小

方法
方法名描述
BufferedWriter(write)创建字符缓冲输出流对象
BufferedReader(reader)创建字符缓冲输入流对象
readLine()读取一行内容并返回字符串
newLine()转换下一行
public class CopyDemo {

	public static void main(String[] args) {
		long start  = System.currentTimeMillis();
		try {
			//单个字节
//			method1();
//			method2();
			//使用缓冲流读取一行内容
			method3();
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("消耗时间为:"+(end-start));
	}

	private static void method3() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader("E:\\Java\\广州新闻.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\Java\\广州小说.txt"));
		String str;
		//readLine可以直接读取一行字符串
		while((str=br.readLine())!=null) {
			bw.write(str);  //可以直接写出一行
			bw.newLine(); //换行
		} 
		bw.close();
		br.close();
	}

	private static void method2() throws Exception {
		Reader r = new FileReader("E:\\Java\\广州新闻.txt");
		Writer w = new FileWriter("E:\\Java\\广州小说.txt");
		int c;
		while((c=r.read())!=-1) {
			w.write(c);
		}
		r.close();
		w.close();
	}

	//单个字符读取
	private static void method1() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader("E:\\Java\\广州新闻.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\Java\\广州小说.txt"));
		int c;
		while((c=br.read())!=-1) {
			bw.write(c);
		}
		br.close();
		bw.close();
	}
}

打印流

​ 打印流分: 字节打印流PrintStream 字符打印流PrintWriter

​ 打印流的特点:

​ 只负责打印输出数据,不负责读取数据

​ 永远不会抛出IOException

​ 有自己的特有方法

public class PrintStreamDemo {

	public static void main(String[] args) throws Exception {
		//创建对一个打印流对象
		PrintStream out = new PrintStream("E:\\Java\\小说.txt");
		//写出数据
		out.print("helloworld");
		out.print(97);
		//释放资源
		out.close();
	}
}

​ 字符打印流

public class CharPrintDemo {

	public static void main(String[] args) throws Exception {
		// 创建字符打印流对象
//		PrintWriter out = new PrintWriter("E:\\Java\\小说.txt");
//		out.write("今天天气不错,凉快!");
//		out.println();
//		out.print("Hello");
//		out.close();
		//每次调用会被刷新
		PrintWriter out = new PrintWriter(new FileWriter("E:\\Java\\小说.txt"),true);
		out.write("今天天气不错,凉快!");
		out.println();
		out.print("Hello");
		out.close();
	}
}

对象序列化流

概念

​对象序列化流: 就是将对象永久的保存在磁盘中,或者在网络中传输对象

​这种机制就是使用一个字节序列表示一个对象,该字节序列包含: 对象的类型,对象的数据和对象中存储的属性等信息

​字节序列化: 字节序列写到文件中,相当于文件保存一个对象信息

​反序列化: 该字节从文件中共读取回来,还原为对象叫做反序列化

对象序列化流: ObjectOutputStream

​将java对象的原始数据类型和图形写入到OutputStream.可以使我们的ObjectOutputStream读取对象,使用文件保存,在另一个计算机也可以使用

构造方法

​ObjectOutputStream(outStream) 创建一个对象输出流,输出对象到指定文件

​ 方法

​ writeObject(对象) 将对象写入到文件 类型成为Object

注意: 在需要被序列化的类中必须实现Serializable 可序列化接口

public class Student implements Serializable {

	private static final long serialVersionUID = 1L;

	private String name;
	private String sex;
	private int age;
	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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
	}
}

public class ObjectOutDemo {

	public static void main(String[] args) throws Exception {
		//创建序列化流
		ObjectOutputStream oos = new           
            
            	ObjectOutputStream(new FileOutputStream("stu.txt"));
		//准备需要被序列化的对象
		Student stu = new Student();
		stu.setName("张三");
		stu.setSex("男");
		stu.setAge(28);
		//通过对象序列化流将对象输出到文件
		oos.writeObject(stu);
		System.out.println("保存成功");
		oos.close();
	}
}
对象反序列化流: ObjectInputStream

​ObjectInputStream反序列化先前用ObjectOutputStream编写的原始数据和对象,还原为回来的过程

构造方法

​ObjectInputStream(inStream) 创建从指定的InputStream中读取的反序列化流对象

​ 方法

​ readObject() 读取字节序列还原为对象Object

public class ObjectInputDemo {

	public static void main(String[] args) throws Exception {
		//创建反序列化流
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
		//读取字节序列化内容为Object对象
		Object obj = ois.readObject();
		Student stu = (Student) obj;
		System.out.println("学生姓名为:"+stu.getName()+", 性别为:"+stu.getSex()+", 年龄为:"+stu.getAge());
		ois.close();
	}
}

以集合的形式序列化对象

public class ListOutStream {

	public static void main(String[] args) throws Exception {
		// 创建输出流对象(序列化)
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stus.txt"));
		// 创建输入流对象(反序列化)
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stus.txt"));
		// 准备集合数据
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("张安", "男", 22));
		list.add(new Student("李安", "男", 23));
		list.add(new Student("赵柳", "女", 18));
		list.add(new Student("王辉", "男", 21));
		// 使用序列化流输出对象
		oos.writeObject(list);
		// 当需要使用集合时,将集合返回
		List<Student> list1 = (List<Student>) ois.readObject();
		for (Student stu : list1) {
			System.out.println(stu);
		}
		oos.flush();
		oos.close();
		ois.close();
		addStu(list);
	}

	private static void addStu(List<Student> list) throws Exception {
		// 创建输出流对象(序列化)
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stus.txt"));
		// 创建输入流对象(反序列化)
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stus.txt"));
		list.add(new Student("赵慧", "女", 18));
		oos.writeObject(list);

		List<Student> list1 = (List<Student>) ois.readObject();
		for (Student stu : list1) {
			System.out.println(stu);
		}
	}

}

Properties集合

​ Properties作为Map集合使用

​ 是一个Map体系的集合1

​ properties可以保存到流中,可以从流中读取数据

​ 属性列表中每个键对应一个字符串

Properties的基本使用

public class PropertiesDemo {
	//Map操作方式
	public static void main(String[] args) {
		//创建属性集合对象,Map操作方式
		Properties pro = new Properties();
		//存储元素
		pro.put("driver", "com.mysql.jdbc.Driver");
		pro.put("name", "root");
		
		//遍历集合
		for (Object key : pro.keySet()) {
			System.out.println("key:"+key+"======值:"+pro.get(key));
		}
	}
}
Properties和IO流结合使用(应用)

​ 步骤: 1.在项目中创建properties属性文件

​ 2.在属性文件中 属性=值 的配置

​ 3.使用流的方式读取文件为流

​ 4.使用properties的load方法,将流中的属性加载到Properties对象中

​ 5.获取属性

​ 6.关闭资源

#在src文件夹下创建jdbc.properties文件

###驱动
#driver=com.mysql.jdbc.Driver
#name=root
#password=12345


driver=org.oracle.jdbc.Driver
name=sccot
password=12345

代码读取属性文件

public class PropertiesDemo {
	public static void main(String[] args) throws Exception {
		//创建属性集合对象
		Properties pro = new Properties();
		//创建流读取文件为流
		//PropertiesDemo.class.getClassLoader()  获取到当前类所在
		//getResourceAsStream 从当前类所在资源文件夹中读取文件并转换为Stream
		InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("jdbc.properties");
		//将流加载到properties
		pro.load(in);
		//从属性集合中拿出内容
		System.out.println("驱动为:"+pro.getProperty("driver")+", 账户:"+pro.getProperty("name")+", 密码:"+pro.getProperty("password"));
		in.close();
	}
}

向属性文件中输出内容

public class PropertiesDemo2 {
	public static void main(String[] args) throws Exception {
		//创建属性集合对象
		Properties pro = new Properties();
		pro.setProperty("重庆", "火锅,重庆小面,重庆轻轨,重庆串串");
		pro.setProperty("广东", "穿拖鞋的房东,牛肉丸,龙眼,荔枝");
		//通过字符流输出
		Writer out = new FileWriter("src\\jdbc.properties");
		pro.store(out, null);
		out.close();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢木木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值