36.其他流

其他流

1.序列流

1.什么是序列流

  • 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.

2.使用方式

package heima_day22;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class Demo1_otherIO {
    @throws IOException 
	public static void main(String[] args) throws IOException {
		//Demo1();
		//Demo2();整合两个输入流
		//整合多个输入流
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		FileInputStream fis3 = new FileInputStream("c.txt");
		Vector<FileInputStream> v = new Vector<>();//创建集合对象
		v.add(fis1);//将流对象存储起来
		v.add(fis2);
		v.add(fis3);
		
		Enumeration<FileInputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);   //将枚举中的输入流整合成一个
		FileOutputStream fos = new FileOutputStream("d.txt");
		sis.close();
		fos.close();
	}

	private static void Demo2() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		SequenceInputStream sis = new SequenceInputStream(fis1,fis2);
		FileOutputStream fos = new FileOutputStream("c.txt");
		
		int b;
		while((b = sis.read()) != -1) {
			fos.write(b);
		}
		sis.close();
		fos.close();
	}

	private static void Demo1() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");//创建字节输入流关联a.txt
		
		FileOutputStream fos = new FileOutputStream("c.txt");//创建字节输入流关联c.txt
		
		int b1;
		while ((b1 = fis1.read()) != -1) {//不断在a.txt上读取字节
			fos.write(b1);//将读取的字节写到c.txt
		}
		fis1.close();//关闭流
		
		FileInputStream fis2 = new FileInputStream("b.txt");
		int b2;
		while((b2 = fis2.read()) != -1) {
			fos.write(b2);
		}
		
		fis2.close();
		fos.close();
	}

}
2.内存输出流

1.什么是内存输出流

  • 该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据

2.使用方式

package heima_day22;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;



public class Demo2_OtherIO {
	/**
	* 2.使用方式
		* 创建对象: new ByteArrayOutputStream()
		* 写出数据: write(int), write(byte[])
		* 获取数据: toByteArray()
		* 
		* ByteArrayOutputStream
		* 内存输出流
		* 
		* FileInputStream读取中文的时候出现乱码
		* 
		* 解决方案
		* 1,字符流读取
		* 2,ByteArrayOutStream
	 * 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("e.txt");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();//在内存中创建了可以增长的内存数组
		
		int b;
		while((b = fis.read())!= -1) {
			baos.write(b);
		}
		//byte[] arr= baos.toByteArray();//将缓冲区的数据全部取出,并赋值给arr数组
		//System.out.println(new String(arr));
		
		System.out.println(baos.toString());//将缓冲区的内容转换为字符串,在输出语句中可以省略调用toString方法
		fis.close();
	}

}

3.随机访问流概述和读写数据

随机访问流概述

  • RandomAccessFile概述
  • RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
  • 支持对随机访问文件的读取和写入。
package heima_day22;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo3_randomIO {

	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("g.txt", "rw");
		//raf.write(97);
		//int x = raf.read();
		//System.out.println(x);
		
		raf.seek(0);//设置指针,设定指定位置读和写,用于多线程下载
		raf.write(98);
		raf.close();

	}

}

4.对象操作流ObjecOutputStream

1.什么是对象操作流

  • 该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作.

2.使用方式

package heima_day22;

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 com.heima.bean.Person;

public class Demo4_ObjecIO {
	/**
	 * 什么是对象操作流
	 	* 该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序
	 *玩游戏
	 *序列化:存档的过程
	 *反序列化:读档的过程
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		//序列化
		Person p1 = new Person("张三",23);
		Person p2 = new Person("李四",24);	

		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
		oos.writeObject(p1);
		oos.writeObject(p2);
		
		oos.close();
		//反序列化
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
		
		Person p3 = (Person) ois.readObject();
		Person p4 = (Person) ois.readObject();
		
		System.out.println(p3);
		System.out.println(p4);
		
		ois.close();
	}

}

优化

package heima_day22;

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

import com.heima.bean.Person;

public class Demo5_IO {
	/**
	 * 将对象存储在集合中写出
	 * @param args
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {

		Person p1 = new Person("张三", 23);
		Person p2 = new Person("李四", 24);
		Person p3 = new Person("马哥", 18);
		Person p4 = new Person("辉哥", 20);
		
		ArrayList<Person> list = new ArrayList<>();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		list.add(p4);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));
		oos.writeObject(list);									//写出集合对象
		
		oos.close();
		
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt"));
		@SuppressWarnings("unchecked")
		ArrayList<Person> list1 = (ArrayList<Person>)ois.readObject();	//泛型在运行期会被擦除,索引运行期相当于没有泛型
	//想去掉黄色可以加注解			
	
		for (Person person : list) {
			System.out.println(person);
		}
	
	ois.close();

	}

}

5.数据输入输出流

1.什么是数据输入输出流

  • DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据
  • 例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.

2.使用方式

package heima_day22;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo6_IO {
	public static void main(String[] args) throws IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
		dos.write(997);
		dos.write(998);
		dos.write(999);
		dos.close();
		DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));
		int x = dis.readInt();
		int y = dis.readInt();
		int z = dis.readInt();
		System.out.println(x);
		System.out.println(y);
		System.out.println(z);
		dis.close();
	}
}
6.打印流的概述和特点
  • 1.什么是打印流

* 该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式

* System.out就是一个PrintStream, 其默认向控制台输出信息

  • 2.使用方式
package heima_day22;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import com.heima.bean.Person;
public class Demo7_IOprintln {

	public static void main(String[] args) throws FileNotFoundException {
		//Demo1();
		PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);
		pw.println(97);//自动刷出功能只针对println方法,把缓冲区的字节刷出
		//pw.write(97);
		
		pw.close();//流能把缓冲区的字符刷出,但是流需要关,所以自动刷出功能意义不大
	}

	private static void Demo1() {
		PrintStream ps = System.out;//获取标准输出流
		ps.println(97);//其实底层用的是Integer.toString(x),将x转换为数字字符串打印
		ps.write(97);//查找码表,找到对应的a并打印
		
		Person p1 = new Person("avc",23);
		ps.println(p1);//默认调用p1的toString方法
		
		Person p2 = null;//打印引用数据类型,如果是null,就打印null,如果不是null,就打印对象的toString方法
		ps.println(p2);
		ps.close();
	}

}

7.标准输入输出流概述和输出语句

1.什么是标准输入输出流

  • System.in是InputStream, 标准输入流, 默认可以从键盘输入读取字节数据
  • System.out是PrintStream, 标准输出流, 默认可以向Console中输出字符和字节数据

2.修改标准输入输出流

  • 修改输入流: System.setIn(InputStream)
  • 修改输出流: System.setOut(PrintStream)
package heima_day22;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class Demo8_System_inout {

	public static void main(String[] args) throws IOException {
		//Demo1();
		System.setIn(new FileInputStream("a.txt"));//改变标准输入流
		System.setOut(new PrintStream("b.txt"));//改变标准输出流
		
		InputStream is =System.in;//获取标准的键盘输入流,默认指向键盘,改变后指向文件
		PrintStream ps =System.out;//获取标准输出流,默认指向的是控制台,改变后指向文件
		
		int b;
		while((b = is.read()) != -1) {//从a.txt上读取数据
			ps.write(b);//将数据写到b.txt上
		}
		
		is.close();
		ps.close();
	}

	private static void Demo1() throws IOException {
		InputStream is = System.in;
		int x = is.read();
		System.out.println(x);
		
		//is.close();
		//输入流只有一个,关流后,即使重新创建一个流,也无法使用。
		//并没有关联文件,所以即使不关流也没有问题
	}

}

8.两种方式实现键盘录入

A:BufferedReader的readLine方法。

  • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

B:Scanner

package heima_day22;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Demo9_Scanner {
	//两种方式实现键盘录入
	public static void main(String[] args) throws IOException {
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));//InputStream转换流
		String line = br.readLine();
		System.out.println(line);
		br.close();
		
		Scanner sc = new Scanner(System.in);
		String line1= sc.nextLine();
		System.out.println(line1);
		sc.close();
	}

}

9.Properties的概述
package heima_day22;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class Demo10_properties {
	/**
	 * A:Properties的概述
			* Properties 类表示了一个持久的属性集。
			* Properties 可保存在流中或从流中加载。
			* 属性列表中每个键及其对应值都是一个字符串。 

		* A:Properties的特殊功能
			* public Object setProperty(String key,String value)
			* public String getProperty(String key)
			* public Enumeration<String> stringPropertyNames()

		* A:Properties的load()和store()功能
		* B:案例演示
			* Properties的load()和store()功能
	 * @throws IOException 
	 * @throws FileNotFoundException 

	 * 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//Demo1();
		//Demo2();
		Properties prop = new Properties();
		System.out.println("读取前:"+prop);
		prop.load(new FileInputStream("config.properties"));
		System.out.println("读取后"+prop);
		prop.setProperty("tel", "888888");
		System.out.println("修改后"+prop);//配置文件并没有改变
		prop.store(new FileOutputStream("config.properties"), null);//第二个参数是用来描述文件列表的,如果不描述可以传null
		System.out.println("修改后"+prop);//配置文件修改了
	}

	private static void Demo1() {
		//Properties作为Map集合的使用
		Properties prop = new Properties();
		prop.put("abc",123);
		System.out.println(prop);
	}
	
	private static void Demo2() {
		Properties prop = new Properties();
		prop.setProperty("name", "张三");
		prop.setProperty("tel", "1234124123");
		
		//System.out.println(prop);
		//遍历
		Enumeration<String> en = (Enumeration<String>) prop.propertyNames();//没有泛型,所以需要强转
		while(en.hasMoreElements()) {
			String key =en.nextElement();//获取Properties中的每一个键
			String value = prop.getProperty(key);//根据键获取值
			System.out.println(key+"----"+value);
		}
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值