数据输入输出流的概述和使用
- 数据输入输出流:特点就是能够读写基本数据类型
数据输入流: DataInputStream数
据输出流: DataOutputStream
public class MyTest {
public static void main(String[] args) throws IOException {
// 数据输入输出流:特点就是能够读写基本数据类型
// writeData();
//注意读取的顺序,刚才怎么写的,就怎么读
DataInputStream in = new DataInputStream(new FileInputStream("a.txt"));
boolean b = in.readBoolean();
double v = in.readDouble();
int i = in.readInt();
char c = in.readChar();
String s = in.readUTF();
System.out.println(b);
System.out.println(v);
System.out.println(c);
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(1000);
out.writeChar('a');
out.writeUTF("薛晓燕");
out.flush();
out.close();
}
}
内存操作流的概述和使用
- 操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭 - 操作字符数组
CharArrayWrite
CharArrayReader - 操作字符串
StringWriter
StringReader
- 代码演示
- 操作字节数组
`public class MyTest2 {
public static void main(String[] args) throws IOException {
//内存操作流,在内存中进行读写操作,不关联文件
ByteArrayOutputStream //此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据。
//
//关闭 ByteArrayOutputStream 无效。
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("今天是个好日子".getBytes());
out.write("今天我要嫁给你了".getBytes());
//取出他缓存中的数据
byte[] bytes = out.toByteArray();
String s = new String(bytes);
System.out.println(s);
String s2 = out.toString();
System.out.println(s);
out.close();//此流无需关闭
}
- 操作字符数组
public class MyTest4 {
public static void main(String[] args) throws IOException {
//操作字符数组
//CharArrayWrite
//CharArrayReader
CharArrayWriter charArrayWriter =new CharArrayWriter();
charArrayWriter.write("abcd");
charArrayWriter.write(new char[]{'我','爱','你'});
char[] chars = charArrayWriter.toCharArray();
String s1 = new String(chars);
String s2 = String.valueOf(chars);
System.out.println(s1);
System.out.println(s2);
String s = charArrayWriter.toString();
System.out.println(s);
}
}
- 操作字符串
public class MyTest5 {
public static void main(String[] args) {
//操作字符串
// StringWriter
//StringReader
StringWriter stringWriter = new StringWriter();
stringWriter.write("abc");
stringWriter.write("呵呵呵呵呵");
String s = stringWriter.toString();
System.out.println(s);
}
}
- 利用内存操作流把两首歌合成一首歌
public class CopyMp3 {
public static void main(String[] args) throws IOException {
FileInputStream in1 = new FileInputStream("上海滩.mp3");
FileInputStream in2 = new FileInputStream("曾经的你.mp3");
FileOutputStream out = new FileOutputStream("歌曲大连唱.mp3");
ArrayList<FileInputStream> list = new ArrayList<>();
list.add(in1);
list.add(in2);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
for (FileInputStream inputStream : list) {
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
}
//取出内存中的字节数组
byte[] bytes1 = byteArrayOutputStream.toByteArray();
System.out.println(bytes1.length);
ByteArrayInputStream stream = new ByteArrayInputStream(bytes1);
byte[] bytes2 = new byte[1024];
int len3 = 0;
while ((len3 = stream.read(bytes2)) != -1) {
out.write(bytes2, 0, len3);
}
in1.close();
in2.close();
out.close();
}
}
- 注意:这种方法不能合成太大的文件,因为占用的是内存资源,而内存是有限的
随机访问流RandomAccessFile
- 随机访问流概述
RandomAccessFile概述 最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
RandomAccessFile的父类是Object, 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据
public class MyTest2 {
public static void main(String[] args) throws IOException {
writeData();
RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
raf.seek(0);//可以设置文件指针的位置
boolean b = raf.readBoolean();
int i = raf.readInt();
String s = raf.readUTF();
System.out.println(b);
System.out.println(i);
System.out.println(s);
System.out.println(raf.getFilePointer()); //获取文件指针目前处于的位置
//可以移动文件指针
System.out.println("---------------------");
raf.seek(0);//可以设置文件指针的位置
b = raf.readBoolean();
i = raf.readInt();
s = raf.readUTF();
System.out.println(b);
System.out.println(i);
System.out.println(s);
return;
}
private static void writeData() throws IOException {
RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
raf.writeBoolean(false);
raf.writeInt(1000);
raf.writeUTF("你好"); //会多写两个字节
raf.close();
}
}
序列化流和反序列化流的概述和使用
- 序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
- 注意:此对象 要重写Serializable 接口才能被序列化
public class MyTest {
public static void main(String[] args) throws IOException {
Student student = new Student("战三", 30);
Student student1 = new Student("战三1", 301);
Student student2 = new Student("战三2", 302);
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("student.txt"));
//将三个对象写入
outputStream.writeObject(student);
outputStream.writeObject(student1);
outputStream.writeObject(student2);
//读取文件里面的对象
//方法一:按顺序取出两个对象,并打印
Object obj = stream.readObject();
Student student = (Student) obj;
System.out.println(student.getName() + "==" + student.getAge());
Object obj2 = stream.readObject();
Student student2 = (Student) obj2;
System.out.println(student2.getName() + "==" + student2.getAge());
//方法二:将所有的对象存入集合然后将集合序列化,最后对集合进行反序列化,然后读取
// ArrayList<Student> list = new ArrayList<>();
// list.add(student);
// list.add(student1);
// list.add(student2);
//
// ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("list.txt"));
// outputStream.writeObject(list);
// ObjectInputStream stream = new ObjectInputStream(new FileInputStream("list.txt"));
// Object obj = stream.readObject();
// ArrayList<Student> list= (ArrayList<Student>) obj;
// //Student student = list.get(2);
// for (Student student : list) {
// System.out.println(student.getName()+"=="+student.getAge());
// }
}
//Serializable 一个对象可被序列化的一个标记接口
class Student implements Serializable {
private static final long serialVersionUID = 5760262756605700379L;
//生成一个类的唯一id
private String name;
//transient 修饰成员变量后,此成员变量的就不会序列化到文件中
//transient private int age;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
}