IO流之3

******#******01-IO流(序列流)
*1,什么是序列流
*序列流可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始读
读完一个之后继续读第二个,以此类推.
*2,整合方法;
*整合两个SequenceInputStream(InputStream,InputStream)
FileInputStream file1 = new FileInputStream(“a”);
FileInputStream file2 = new FileInputStream(“b”);
SequenceInputStream sis = new SequenceInputStream(file1, file2);
FileOutputStream fos =new FileOutputStream(“c.txt”);
int b;
while((b=sis.read())!=-1){
fos.write( b);
}
sis.close();
fos.close();
#02-IO流(序列流整合多个)(了解)
*整合多个:SequenceInputStream (Enumeration)
FileInputStream file1 = new FileInputStream(“a”);//创建输入流对象,关联“a”
FileInputStream file2 = new FileInputStream(“b”);//创建输入流对象,关联“b”
FileInputStream file3 = new FileInputStream(“c”);//创建输入流对象,关联“c”
Vector v = new Vector<>(); //创建Vector集合对象
v.add(file1); //将流进行添加
v.add(file2);
v.add(file3);
Enumeration en = v.elements(); //获取枚举引用
SequenceInputStream sis = new SequenceInputStream(en);//传递给SequenceInputStream
FileOutputStream fos = new FileOutputStream(“mnk.txt”);
int b;
while ((b= sis.read())!=-1){
fos.write( b);
}
sis.close();
fos.close();
#03-IO流(内存输出流)(掌握)
*1,什么是内存输出流
*ByteArrayOutputStream此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长,此流可以不用关闭
*给输出流可以向内存中写数据,把内存当作一个缓冲区,写出之后可以一次性获取所有数据
2,使用方式
*创建对象:new ByteArrayOutputStream();
*写出数据:write(int),write(byte [])
*获取数据:toByteArray()
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(“a.txt”);
ByteArrayOutputStream bis =new ByteArrayOutputStream();
int b;

	while((b=fis.read())!=-1){
		bis.write(b);
	}
	System.out.println(bis.toString());
	fis.close();
	

} 

#04-IO流(内存输出流面试题)
*写出一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组的大小限制为5)
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(“a.txt”);
//创建内存输出流,将文件读到内存输出流中
ByteArrayOutputStream bis =new ByteArrayOutputStream();
//创建字节数组,长度为5
byte []arr = new byte [5];
int b;

	while((b=fis.read(arr))!=-1){
		bis.write(arr,0,b);
	}
	//将内存输出流的数据全部转换为字符打印
	System.out.println(bis);
	fis.close();
	

}

#05-IO流(对象操作流)

主类:
Person p1 = new Person(“张三”,23);
ObjectOutputStream op = new ObjectOutputStream(new FileOutputStream(“jdsjf.txt”)) ;
op.writeObject(p1);
op.close();
调用的类:

public class Person implements Serializable {//必须实现Serializable接口才能被序列化

private String name;
private int age;
public Person(String string, int i){
	super();
}
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 "Person [name=" + name + ", age=" + age + "]";
}

#07-IO流(对象操作流ObjectInputStream)

    ObjectInputStream oi = new ObjectInputStream(new FileInputStream("jdsjf.txt"));
	Person p = (Person) oi.readObject();
	System.out.println(p);
	oi.close();

#08-IO流(对象操作流优化)
*将对象储存在集合中写出

    Person p1 = new Person("张三",23);
	Person p2 = new Person("李四",24);
	ObjectOutputStream op = new ObjectOutputStream(new FileOutputStream("jdsjf.txt")) ;
	ArrayList<Person> a = new ArrayList<>();
	a.add(p1);
	a.add(p2);
	op.writeObject(a);
	op.close();

*读取到的是一个集合对象
ObjectInputStream oi = new ObjectInputStream(new FileInputStream(“jdsjf.txt”));
ArrayList a2 = (ArrayList)oi.readObject();
for(Person p: a2){
System.out.println§;
}
oi.close();
}
#08-IO流(加上id号)
*注意
*要写出的对象必须实现Serializable接口才能被序列化
*不用必须加上序列化
#09-IO流(打印流的概述和特点)
*1,什么是打印流
*该流可以很方便的将对象的toString()结果输出,并且自动加上换行,而且可以使用自动输出模式
*System.out就是一个PrintStream,其默认向控制台输出信息
*2,打印方式
*打印:print(),println()
*自动刷出:PrintWriter(OutputStream out,boolean autoFlush(自动刷新),String encoding )
*打印流只操作数据目的
#10-IO流(标注输入输出流概述和输出语句)
*1,修改标注输入输出流
*修改输入流System.setIn(new FileInputStream(“a.txt”));
*修改输出流System.setOut(new PrintStream(“m.txt”));

	System.setIn(new FileInputStream("a.txt"));//修改标准输入流
	System.setOut(new PrintStream("m.txt"));   //修改标注输出流

	InputStream is = System.in;                //获取标准输入流
	PrintStream ps = System.out;               //获取标准输出流

	int b;
	while((b=is.read()) !=-1){                 //从a.txt上读取数据
		ps.write(b);                           //将数据写到m.txt上
	}

#11-IO流(两种方式实现键盘录入)
*A:BufferedReader和readLine方法
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.println(s);
br.close();
*B:Scanner
#12-IO流(随机访问流概述和读写数据)(掌握)
*A:随机访问流概述:
*RandomAccessFile概述:
RandomAccessFile类不属于流,是object的子类,但是他融合了InputStream和OutputStream的功能,
支持对随机访问文件的读取和写入
*B:read(),write(),seek()(随机访问rf.seek(2);从0开始数替代第二个)

    RandomAccessFile rf = new RandomAccessFile("g.txt", "rw");//rw指既可以又可以写
	rf.write(99);
	rf.seek(2);
	rf.write(99);
    rf.close();

#13-IO流(数据的输入输出流)
*1,什么是数据输入输出流
*DataInputStream,DateOutputStream 可以按照基本数据类型大小写读取数据
*例如按照long大小写出一个数字写出该数据站8个字节,都去的时候也可以按照long类型读取,一次读取八个字节
*2,使用方式
*DateOutputStream(OutputStream),writeInt(),writelong()
DataOutputStream ds = new DataOutputStream(new FileOutputStream(“l.txt”));
ds.writeInt(566);
ds.close();
*DataInputStream(InputStream),readInt(),readLong()

	DataInputStream di = new DataInputStream(new FileInputStream("l.txt"));
	int s = di.readInt();
	System.out.println(s);
	di.close();

#14-IO流(Properties的概述和作为Map集合的使用)
*A:Properties 的概述:
*Properties类表示了一个持久的属性集
*Properties可保存在流中或从流中加载
*属性列表中每个键值对对应值都是一个字符串
*B:案例演示:
Properties p = new Properties();
p.put(“abc”, 123);
System.out.println§;
#15-IO流(Properties的特殊功能使用)
*A:Properties的特殊功能
p.setProperty(“张三”, “23”);
p.getProperty(key);//获取值
(Enumeration) p.propertyNames();
*B:案例演示:

	Properties p = new Properties();
	p.setProperty("张三", "23");
	p.setProperty("tell", "12346");
    //(Enumeration<String>)强转是因为Properties默认的是String
    Enumeration<String > e = (Enumeration<String>) p.propertyNames();
    while(e.hasMoreElements()){
    	String key = e.nextElement();//获取Properties中的每一个键
    	String value = p.getProperty(key);//获取值
    	System.out.println(key+"="+value);
    }

#16-IO流(Properties的load()和store()功能)
*A:Properties的load()和store()功能
*B:案例演示

	Properties p = new Properties();
	p.load(new FileInputStream("file.properties"));//将文件中的内容读取到集合中
	p.setProperty("tell", "123");   //修改指定的输出文件,但是不会对文件产生修改
	p.store(new FileOutputStream("file.properties"), null); //写入文件,第二个参数是对文件的一份描述,可以写值,也可以写null
    System.out.println(p);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值