数据输入输出流
DataInputStream数据输入流,让应用程序读取原始java数据类型从底层输入流中的一个独立机器的方式。一个应用程序使用一个数据输出流来写数据,以后可以通过数据输入流读取。输入流是不一定安全的多线程访问。线程安全是可选的,是在这个类中的方法的用户的责任。
DataOutputStream数据输出流,可以让一个应用java写的原始数据类型在一个便携式的方式输出流。一个应用程序可以使用一个数据输入流来读取数据。
特点:此流最大的特点是能够读写基本数据类型。
案例:
import java.io.*;
public class Demo6 {
public static void main(String[] args) throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream("C:\\file.txt"));
out.writeInt(100);
out.writeChar('a');
out.writeDouble(3.14);
out.writeBoolean(true);
DataInputStream in = new DataInputStream(new FileInputStream("C:\\file.txt"));
int i = in.readInt();
char c = in.readChar();
double v = in.readDouble();
boolean b = in.readBoolean();
System.out.println(i);
System.out.println(c);
System.out.println(v);
System.out.println(b);
}
}
注意:顺序不能乱,怎么写入的顺序就怎么读取。
内存操作流
内存操作流,是在内存对数据进行操作,所以不需要关联文件,会把数据写入内存中,同样也会从内存中读取数据
- 操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭 - 操作字符数组
CharArrayWrite
CharArrayReader - 操作字符串
StringWriter
StringReader
案例:
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("At the mess you made".getBytes());
out.write("Dont want to let you down".getBytes());
out.write("But I am hell bound".getBytes());
byte[] bytes = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024 * 8];
int len=in.read(bytes1);
System.out.println(new String(bytes1,0,len));
}
}
打印流
打印流分为字节打印流和字符打印流
打印流的特点:
- 打印流只能操作目的地,不能操作数据源。就是不能进行读取数据
- 可以操作任意数据类型的数据,使用print()方法。
- 可以启动自动刷新,不用我们手动flush()。但是只有调用println、printf 或 format方法中的一个方法时,才会自动刷新。
- 此流可以直接对文件进行操作
构造方法:
- PrintStream(File file)
创建一个字节打印流,传入File类型 - PrintStream(OutputStream out, boolean autoFlush)
创建一个字节打印流,传入一个OutputStream子类对象。第二个参数时是否启动自动刷新 - PrintStream(String fileName)
创建一个字节打印流,传入一个指定文件名 - public PrintWriter(OutputStream out, boolean autoFlush)
创建一个字符打印流,是否启动自动刷新 - public PrintWriter(Writer out, boolean autoFlush)
创建一个字符打印流,是否启动自动刷新
案例:
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(new FileWriter("File.txt"), true);
printWriter.println("Trouble I'm In");
printWriter.println(100);
printWriter.println(3.14);
printWriter.println('x');
}
}
序列化流和反序列化流
序列化指的是将对象通过流的方式存储在文件中
反序列化是指将文件中存储的对象通过流的方式还原成对象
此流最大的特点就是能读取和写入对象。但是此对象要重写Serializable 接口才能被序列化。Serializable 是一个标识接口,不需要重写方法。
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
案列:
import java.io.*;
public class Demo3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("File.txt"));
Student student1 = new Student("张三", 23);
Student student2 = new Student("李四", 25);
Student student3 = new Student("王五", 30);
out.writeObject(student1);
out.writeObject(student2);
out.writeObject(student3);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.txt"));
for (int i = 0; i < 3; i++) {
Object o = in.readObject();
Student str=(Student) o;
System.out.println(str.getName()+"=="+str.getAge());
}
out.close();
in.close();
}
}
import java.io.Serializable;
public class Student implements Serializable {
public static final long serialVersionUID = 42L;
String name;
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;
}
}
Properties
Properties类似于HashMap,都是双列集合,键值默认都是字符串类型。
Properties的特有方法:
- public Object setProperty(String key,String value)
向该集合中添加键值对 - public String getProperty(String key)
键找值 - public Set stringPropertyNames()
获取所有的键的枚举 - public void load(Reader reader)
读取键值对数据把数据存储到Properties中 - public void store(Writer writer, String comments)
把Properties集合中的键值对数据写入到文件,comments是注释
Scanner
我们以前在录入数据时使用到的Scanner scanner = new Scanner(System.in),其实我们传入的也是一个输入流,其中in对应的就是键盘,所以我们完全可以使用System.in作为输入流来使用。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileInputStream("file"));
PrintWriter printWriter = new PrintWriter(new FileOutputStream("file2.txt"));
while (scanner.hasNext()){
String s=scanner.nextLine();
printWriter.println(s);
}
scanner.close();
printWriter.close();
}
}
随机访问流
这个类的实例支持对随机存取文件的读和写。一个随机存取文件的行为像一个大数组的字节存储在文件系统中。有一种光标,或索引到隐含数组,称为文件指针;输入操作读取字节开始在文件指针,并推进文件指针过去的字节读。如果随机存取文件是创建在读/写模式,那么输出操作也可用;输出操作写字节开始在文件指针,并推进文件指针过去的字节写的。写入过去的隐含数组的当前结束的输出操作导致数组被扩展。文件指针可以由 getFilePointer方法读取和设置的 seek方法。
RandomAccessFile并不属于流,它是一个类,是Object的子类。最大的特点就是能读能写,它融合了InputStream和OutputStream的功能,支持对随机访问文件的读取和写入。