一、Java流的概述:
1、Java中,所有输入流类都是抽象类inputStream
字节输入流和抽象类Reader
字符流的子类,而所有的输出流都是抽象类Output Stream
字节输出流和Writer
字符输出流的子类。
Reader类是字符流输入流的抽象类,所有字符输入流的实现都是它的子类
2、在Java中,输出流是OutPut Stream
和Writer
两个类Writer类是字符输出流的抽象类,所有字符输出流的实现都是它的子类
流是一组有序的输出序列数据
二、RandomAccessFile
重点:
想比较之前的流要么读要么写,RandomAccessFile提供了读写操作RandomAccessFile声明在java.io包下,但直接继承于java.lang.Object类。并且它实现了Datalnput、DataOutput两个接口
1、getFilePointer和seek是RandomAccessFile实现随机访问的核心方法
2、RandomAccessFile使用了指针的思想
3、RandomAccessFile可以用来实现多线程下载及断点续传
三、输入流和输出流
1、
public class Input {
public static void main(String[] args) {
char input = '\n';
System.out.println("请开始输入:");
StringBuilder sb = new StringBuilder();
do {
try {
input = (char) System.in.read();
sb.append(input);
} catch (Exception e) {
e.printStackTrace();
}
} while (input != '\n');
System.out.println("请输入的字符串是:" + sb);
}
}
2、
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
System.out.print("请开始输入:");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
System.out.println("请输入的字符串是:"+read);
}
}
3、
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Input {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;
System.out.print("输入数据:");
try {
input = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("输入数据:" + input);
}
}
四、文件输入流和输出流
1、FileRead
import java.io.*;
public class FileRead {
public static void main(String[] args) throws IOException {
String filePath = "test.txt";
File file = new File(filePath);
BufferedReader reader = new BufferedReader(new FileReader(file));
String lineString = null;
while ((lineString = reader.readLine()) != null) {
System.out.println(lineString);
}
reader.close();
}
}
2、FileWriter类是从OutputStreamWriter继承而来
五、对象序列化
import java.io.*;
import java.io.Serializable;
class Student implements Serializable {
private String name;
private String sex;
private Integer age;
public Student(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
return "name:" + this.name + "," + "sex:" + this.sex + "," + "age:" + this.age;
}
}
public class SerializableDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 序列化
Student student = new Student("小明", "男", 15);
System.out.println(student);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student"));
oos.writeObject(student);
oos.close();
// 反序列化
File file = new File("student");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Student student = (Student) ois.readObject();
ois.close();
System.out.println(student);
}
}
六、ZIP文件压缩
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zip {
public static void main(String[] args) throws IOException {
String zipFileName = "test.zip";
String fileName = "test.txt";
File file = new File(fileName);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
out.putNextEntry(new ZipEntry(""));
FileInputStream in = new FileInputStream(file);
int tmp;
while((tmp = in.read()) != -1) {
out.write(tmp);
}
in.close();
out.close();
}
}
七、字节流:InputStream和Output Stream
1、input stream 和outputstream为各种输入和输出字节流的基类,所有字节流都继承这两个基类
2、其他的输入和输出流:
对文件的字节流操作:File Input Stream和FileOutStream
数据输入和输出流:Data Input Stream和DataOutputStream
带缓冲区的输入流和输出流:BufferedInputStream和BufferedOutputStream
字符流: InputStreamReader 和 OutputStreamWriter
1、 InputStreamReader 和 OutputStreamWriter为各种输入输出字符流的基类,所有字符流都继承这两个基类
2、其他输入和输出流:
对读取文件操作系统的封装:FileReader和FileWriter
BufferedReader和BufferedWriter