InputStream(OutputStream)
InputStream(OutputStream)每次只能读写一个字节或一个字
节数组,若要读取或者写入如int,double等数据需要自行处理成字
节再进行读写。
FileInputStream为InputStream接口的一个实现类,方便文件
读取
- 单字节读取
public static void inputStreamTest(String fileName) throws IOException {
FileInputStream fileInputStream = new FileInputStream(fileName);
int i = 0;
while ((i = fileInputStream.read()) != -1) {
System.out.print(Integer.toHexString(i) + " ");
}
fileInputStream.close();
}
//每次写入一个字节
public static void fileOutputStreamTest(String fileName) throws IOException {
FileOutputStream out = new FileOutputStream(fileName, true);
//写出A字符的低8位
out.write('A');
out.write(1);
out.write("哈喽".getBytes());
out.close();
}
- 读取到字节数组
public static void inputStreamTest2(String fileName) throws IOException {
FileInputStream fileInputStream = new FileInputStream(fileName);
byte[] buf = new byte[1024 * 2];
int size = 0;
while ((size = fileInputStream.read(buf)) != -1) {
for (int i = 0; i < size; i++) {
if (i % 10 == 0 && i != 0) {
System.out.println();
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
}
}
fileInputStream.close();
}
//每次读写一个字节数组的数据比读取单个字节效率高很多
public static void copyByBytes(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("source file not exists!");
} else if (!srcFile.isFile()) {
throw new IllegalArgumentException("source file must be file!");
}
FileInputStream inputStream = new FileInputStream(srcFile);
FileOutputStream outputStream = new FileOutputStream(destFile);
byte[] buf = new byte[1024 *1024];
int size = 0;
while ((size = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, size);
outputStream.flush();
}
inputStream.close();
outputStream.close();
}
- DataInputStream的使用
DataOutputStream(DataInputStream)为InputStream
(OutputStream)提供包装,方便读取int,double,UTF等
数据类型
public static void DataInputStreamTest(String fileName) throws IOException {
DataInputStream dataInputStream =
new DataInputStream(new FileInputStream(fileName));
System.out.println(dataInputStream.readInt());
System.out.println(dataInputStream.readDouble());
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readChar());
}
public static void DataOutputStreamTest(String fileName) throws IOException {
DataOutputStream dataOutputStream =
new DataOutputStream(new FileOutputStream(fileName));
dataOutputStream.writeInt(1);
dataOutputStream.writeDouble(1.1);
dataOutputStream.writeUTF("哈喽");
//UTF-16be编码格式,char占两个字节
dataOutputStream.writeChars("哈哈");
dataOutputStream.close();
}
- 使用BufferedInputStream
BufferedInputStream提供了缓冲读取,读取时判断是否可以
从该类自带的缓冲区中读取,提高效率。
多次从流中读取少量字节时候效率较高
使用read(byte[] b)进行读取时,若缓冲区小于b的大小则每次
直接从流中读取,并更新缓冲区,效率与直接使用InputStream
的read(byte[])相当,若缓冲区大小大于b,则效率反而会降低
public static void copyByBuffer(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("source file not exists!");
} else if (!srcFile.isFile()) {
throw new IllegalArgumentException("source file must be file!");
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
byte[] buf = new byte[1024 * 1024];
int c = 0;
while ((c = bis.read(buf)) != -1) {
bos.write(buf, 0, c);
}
bis.close();
bos.close();
}
Reader(Writer)
Reader和Writer简化了字符的读写,可以不用手动对字节进行
处理,而直接面向字符。
- InputStreamReader(Writer)
public static void InputStreamReaderTest(File file) throws IOException {
FileInputStream fi = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fi);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("c.txt")));
int c = 0;
char[] buf = new char[1024];
while ((c = reader.read(buf, 0, buf.length)) != -1){
String s = new String(buf, 0, c);
writer.write(s);
System.out.println(s);
}
reader.close();
writer.close();
}
- FileReader(Writer)
FileReader(Writer)简化了对文件的字符的读写
public static void fileReaderTest(File file) throws IOException {
FileReader fileReader = new FileReader(file);
FileWriter fileWriter = new FileWriter("c.txt");
char[] buf = new char[1024];
int c = 0;
while ((c = fileReader.read(buf, 0, buf.length)) != -1) {
fileWriter.write(buf, 0, c);
}
fileReader.close();
fileWriter.close();
}
- BufferedReader(Writer)
BufferedReader(Writer)对Reader(Writer)进行包装,简化
字符的读写操作。
public static void bufferedReaderTest(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
//PrintWriter更方便
PrintWriter pw = new PrintWriter("d.txt");
String line = "";
while ((line = br.readLine()) != null) {
pw.println(line);
bw.write(line);
bw.newLine();//换行
}
br.close();
bw.close();
pw.close();
}
ObjectInputStream(OutputStream)
需要实现Serializable接口才能进行序列化
没有实现Serializable接口的父类,反序列化时构造函数会被调用
transient关键字指定某个属性不进行序列化,并手动进行序列化
- Student类
public class Student implements Serializable{
private transient int age;
private String name;
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
//手动反序列化
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();//jvm默认可以反序列化的反序列化
age = s.readInt();//默认不行的手动反序列化
}
//手动序列化
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
s.defaultWriteObject();//jvm默认可以序列化的进行序列化
s.writeInt(age);//不能序列化的手动序列化
}
}
- 序列化操作
public static void objectSeriaTest(Student student) throws IOException, ClassNotFoundException {
ObjectOutputStream outputStream = new ObjectOutputStream(
new FileOutputStream("object.txt")
);
ObjectInputStream inputStream = new ObjectInputStream(
new FileInputStream("object.txt")
);
outputStream.writeObject(student);
Student stu = (Student) inputStream.readObject();
System.out.println(stu);
outputStream.close();
inputStream.close();
}