概述
1、数据输入输出流
2、内存操作流
3、打印流
4、序列化流
5、随机访问流
6、Properties
一、数据输入输出流
数据输入和输出流:
数据输入流: DataInputStream
数据输出流: DataOutputStream
特点: 可以写基本数据类型,可以读取基本数据类型
案例演示:
public class 数据输入输出流 {
public static void main(String[] args) throws IOException {
//数据输入输出流的特点,就是能够读写基本数据类型
DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
boolean b = in.readBoolean();
double v = in.readDouble();
int i = in.readInt();
String s = in.readUTF();
System.out.println(b);
System.out.println(v);
System.out.println(i);
System.out.println(s);
in.close();
}
private static void writeData() throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt"));
out.writeBoolean(true);
out.writeDouble(3.14);
out.writeInt(100);
out.writeUTF("你好世界");
out.close();
}
}
二、内存操作流
概述:
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
注意事项:内存操作流,不关联任何文件,只是内存中对数据进行读写
ByteArrayOutputStream
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据。
关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException
public class 内存操作流 {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("横眉冷对千夫指".getBytes());
out.write("俯首甘为孺子牛".getBytes());
/**/取出缓冲区中的数据**
byte[] allBytes = out.toByteArray();
String s = new String(allBytes);
System.out.println(s);
ByteArrayInputStream in= new ByteArrayInputStream(allBytes);
byte[] bytes=new byte[1024*8];
int len = in.read(bytes);
String s1 = new String(bytes, 0, len);
System.out.println(s1);
}
}
三、打印流的概述和特点以及作为Writer的子类使用
打印流的特点
a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据)
b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
1、字节打印流
2、字符打印流
案例演示一
public class 字节打印流 {
public static void main(String[] args) throws IOException {
//打印流:只操作目的地,不关联源文件
// PrintStream 字节打印流
PrintStream printStream = new PrintStream(new FileOutputStream("b.txt"));
printStream.write("字节打印流".getBytes());
printStream.print(true);
printStream.println(100);
printStream.close();
PrintStream out = System.out;
out.write("abc".getBytes());
out.println(3.14);
System.out.println("abc");
}
}
案例演示二、字符打印流
参数2:true 自动刷新
如果启用了自动刷新,则只有在调用 println、printf 或 format 的其中一个方法时才可能完成此操作
public class 字符打印流 {
public static void main(String[] args) throws IOException {
PrintWriter writer = new PrintWriter(new FileOutputStream("c.txt"),true);
//writer.write("字符打印流");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.println("abc");
writer.flush();
writer.close();
}
}
打印流复制文本文件
A:
案例演示:
打印流复制文本文件
public class 打印流复制文本文件 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("内存操作流.java"));
PrintWriter printWriter = new PrintWriter(new FileOutputStream("aa.java"),true);
String line=null;
while ((line=reader.readLine())!=null){
printWriter.println(line);
}
reader.close();
printWriter.close();
}
}
四、 序列化流
概述
序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
比喻:像这样一个接口中如果没有方法,那么这样的接口我们将其称之为标记接口(用来给类打标记的,相当于猪肉身上盖个章)
注意事项:一个对象可以被序列化的前提是这个对象对应的类必须实现Serializable接口
案例演示:
//序列化:把对象保存到,硬盘上
//反序列化:把对象读取到内存中
//ObjectOutputStream 序列化流
// ObjectInputStream 反序列化流
//1.把一个对象,序列化的硬盘上,有个要求,要求该类实现一个Serializable接口,然后该类的对象,才能正常序列化
//2.再实现了Serializable接口之后,最好再写一个 public static final long serialVersionUID = 42L;
public class MyTest3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//writeObj();
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("student1.txt"));
Object obj = objin.readObject();
ArrayList<Student> list= (ArrayList<Student>) obj;
Student student = list.get(2);
System.out.println(student.getName());
}
private static void writeObj() throws IOException {
Student student1 = new Student("zhangsan", 23);
Student student2 = new Student("李四", 24);
Student student3 = new Student("王五", 25);
//如果我们要存储多个对象,我们可以将多个对象,放到集合中,将集合序列化到硬盘上,这样,我们取某个对象就方便
ArrayList<Student> students = new ArrayList<>();
students.add(student1);
students.add(student2);
students.add(student3);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student1.txt"));
out.writeObject(students);
out.close();
}
}
五、随机访问流
概述:
RandomAccessFile概述 最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
我们可以通过getFilePointer方法获取文件指针,并且可以通过seek方法设置文件指针。
注意事项:
RandomAccessFile 随机访问流,此流的特点,能读能写,有一个文件指针,能够记录文件读写的位置
此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;
public static void main(String[] args) throws IOException {
// RandomAccessFile 随机访问流,此流的特点,能读能写,有一个文件指针,能够记录文件读写的位置
// 此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;
//rw 模式,可读可以写
// writeData();
//你怎么写的,就怎么读取 顺序不要乱
RandomAccessFile ra = new RandomAccessFile(new File("e.txt"), "rw");
boolean b = ra.readBoolean();
//获取文件指针的位置
long filePointer = ra.getFilePointer();
System.out.println(filePointer);//1
double v = ra.readDouble();
filePointer = ra.getFilePointer();//***获取指针***
String s = ra.readUTF();
filePointer = ra.getFilePointer();
System.out.println(filePointer);//
System.out.println(s);
//设置指针的位置
ra.seek(15);
String s1 = ra.readUTF();
System.out.println(s1);
}
private static void writeData() throws IOException {
RandomAccessFile ra = new RandomAccessFile(new File("e.txt"), "rw");
ra.writeBoolean(true);
ra.writeDouble(3.14);
ra.writeInt(100);
ra.writeChar('a');
ra.writeUTF("字符串");
ra.close();
}
}
六、 Properties的概述和作为Map集合的使用
Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
注意:- 属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
public class MyTest {
public static void main(String[] args) {
// Properties 属性集合,经常用它来读写配置文件 属于双列集合
Properties properties = new Properties();//他规定了键值 是String类型
//properties.put("aaa","bbb");
//Object aaa = properties.get("aaa");
//System.out.println(aaa);
//用它特有的方法,来存储键值
properties.setProperty("陈羽凡","白百合");
String value = properties.getProperty("陈羽凡");
System.out.println(value);
//参数2,默认值,如果键没有找到对应的值,就返回默认值
String property = properties.getProperty("陈羽凡", "李小璐");
System.out.println(property);
}
}
6.1 Properties的load()和store()功能
public class MyTest2 {
public static void main(String[] args) throws IOException {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
BufferedReader bfr = new BufferedReader(new FileReader("User.properties"));
//username = zhangsan
String s = bfr.readLine();
String[] split = s.split("=");//截取等号
map.put(split[0],split[1]);
String s2 = bfr.readLine();
String[] split2 = s2.split("=");
map.put(split2[0], split2[1]);
System.out.println(map);
Properties properties = new Properties();
//读取配置文件
//要求配置文件键值用等号 = 连接
properties.load(new FileReader("User.properties"));
System.out.println(properties);
}
}