首页 > 其他 > 详细

其他流(常用非基本流)

时间:2020-07-30 18:13:33      阅读:66      评论:0      收藏:0      [点我收藏+]
包装流,也叫缓冲流,高效流
字符输出流,内置数组[1024]
缓冲字节流内置数组[8192]
 
转换流(本身是字符流)是字节流到字符流的桥梁
 
idea默认UTF8编码,而eclipse是gbk
 
 
序列化
注意:序列化对象所在类需先实现Serializable接口
 
重点:
IO流file,四种基本流
缓冲流buffer, 通过缓冲区读写,减少系统IO次数(输入输出次数),从而提高读写的效率。
转换流input/output,解决不同编码的文件转换问题
序列化object,节序列,写出到文件之后,相当于,文件中持久保存了一个对象的信息;反之,该字节序列,还可以从文件中读取回来,重构对象,对它进行反序列化。
 
 
序列化举例
import java.io.*;
import java.util.ArrayList;
 
public class Test05 {
public static void main(String[] args) throws Exception {
//将对象写入文件
//ArrayList<Student> al = new ArrayList();
//al.add(new Student("fbb",18));
//al.add(new Student("zwj", 20));
//
//ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day20\\c.txt"));
//oos.writeObject(al);
//
//oos.close();
 
//从文件中读取对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day20\\c.txt"));
ArrayList<Student> al = (ArrayList<Student>) ois.readObject();
 
ois.close();
 
for (Student student : al) {
System.out.println(student.getName() + student.getAge());
}
}
}
 
class Student implements Serializable { //序列化对象所在类需先实现Serializable接口
private String name;
//private transient int age; //transient瞬时的,无法序列化,不能永久保存
private int age;
private String address;//写入文本后加的成员变量,此时类的class文件已更改,要再次将对象读取到内存,需记录下之前序列号
public static final long serialVersionUID = 6845409090868647L; //将一开始的class文件的序列号记录为一个静态成员常量
 
public Student() {
}
 
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
 
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;
}
 
public String getAddress() {
return address;
}
 
public void setAddress(String address) {
this.address = address;
}
 
@Override
public String toString() {
return "Student{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
", address=‘" + address + ‘\‘‘ +
‘}‘;
}
}
 
 
下面是一些代码:
//缓冲字节流
import java.io.*;

public class Test01 {
public static void main(String[] args) throws Exception {
File f = new File("day20\\b.wmv");
f.delete();

FileInputStream fis = new FileInputStream("day20\\a.wmv");
FileOutputStream fos = new FileOutputStream("day20\\b.wmv");

BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);

long start = System.currentTimeMillis();

int ch;
byte[] arr = new byte[1024];
while ((ch = bis.read(arr)) != -1) {
bos.write(arr, 0, ch);
}

long end = System.currentTimeMillis();

bos.close();//同时也关闭了它所包装的fos
bis.close();//同时也关闭了它所包装的fis

//fos.close();
//fis.close();

System.out.println(end - start);
}
}

//缓冲字符流
//对每一段以“1.先帝创业...”形式开头的出师表进行排序,输出到一个新文本文件中
import java.io.*;
import java.util.HashMap;

public class Test02 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("day20\\a.txt"));
HashMap<String,String> hm = new HashMap();
String ch;
while ((ch = br.readLine()) != null) {
String[] split = ch.split("\\.");
hm.put(split[0], split[1]);
}

br.close();

BufferedWriter bw = new BufferedWriter(new FileWriter("day20\\aa.txt"));

String key = null;
for (int i = 1; i <= hm.size(); i++) {
key = ""+i;
bw.write(key + "." + hm.get(key));
bw.newLine();
}

bw.close();
}
}
上一个代码可改写:
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Test03 {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("day20\\a.txt");
BufferedReader br = new BufferedReader(fr);

ArrayList<String> al = new ArrayList();

String ch;
while ((ch = br.readLine()) != null) {
al.add(ch);
}

br.close();

Collections.sort(al);
BufferedWriter bw = new BufferedWriter(new FileWriter("day20\\aaa.txt"));
for (String s : al) {
bw.write(s);
bw.newLine();
}
bw.close();
}
}

//打印流,用途不大
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Test06 {
public static void main(String[] args) throws Exception {
PrintStream ps = new PrintStream(new FileOutputStream("day20\\d.txt"));
ps.println(666);
ps.write(97);
System.setOut(ps);//改变打印流向
System.out.println(985);
}
}
包装流,也叫缓冲流,高效流
字符输出流,内置数组[1024]
缓冲字节流内置数组[8192]
 
转换流(本身是字符流)是字节流到字符流的桥梁
 
idea默认UTF8编码,而eclipse是gbk
 
 
序列化
注意:序列化对象所在类需先实现Serializable接口
 
重点:
IO流file,四种基本流
缓冲流buffer, 通过缓冲区读写,减少系统IO次数(输入输出次数),从而提高读写的效率。
转换流input/output,解决不同编码的文件转换问题
序列化object,节序列,写出到文件之后,相当于,文件中持久保存了一个对象的信息;反之,该字节序列,还可以从文件中读取回来,重构对象,对它进行反序列化
 
 
序列化举例
import java.io.*;
import java.util.ArrayList;
 
public class Test05 {
public static void main(String[] args) throws Exception {
//将对象写入文件
//ArrayList<Student> al = new ArrayList();
//al.add(new Student("fbb",18));
//al.add(new Student("zwj", 20));
//
//ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day20\\c.txt"));
//oos.writeObject(al);
//
//oos.close();
 
//从文件中读取对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day20\\c.txt"));
ArrayList<Student> al = (ArrayList<Student>) ois.readObject();
 
ois.close();
 
for (Student student : al) {
System.out.println(student.getName() + student.getAge());
}
}
}
 
class Student implements Serializable { //序列化对象所在类需先实现Serializable接口
private String name;
//private transient int age; //transient瞬时的,无法序列化,不能永久保存
private int age;
private String address;//写入文本后加的成员变量,此时类的class文件已更改,要再次将对象读取到内存,需记录下之前序列号
public static final long serialVersionUID = 6845409090868647L; //将一开始的class文件的序列号记录为一个静态成员常量
 
public Student() {
}
 
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
 
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;
}
 
public String getAddress() {
return address;
}
 
public void setAddress(String address) {
this.address = address;
}
 
@Override
public String toString() {
return "Student{" +
"name=‘" + name + \‘ +
", age=" + age +
", address=‘" + address + \‘ +
‘}‘;
}
}

 

其他流(常用非基本流)

原文:https://www.cnblogs.com/21556guo/p/13404961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!