各种流
1.对象流
1.1 利用对象输出流 向文件写入数据ObjectOutputStream
public class Test01 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//1.创建流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io.txt"));
//2.写入数据
oos.writeInt(100);//写入int值
oos.writeDouble(123.123);//写入double值
oos.writeUTF("用良心做教育");//写入字符串
oos.writeObject(new Date());//写入对象
//3.关闭资源
oos.close();
}
}
1.2 利用对象输入流 读取文件里的数据ObjectInputStream
public class Test02 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//1.创建流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("io.txt"));
//2.读取数据(读取顺序必须和写入顺序一致)
int readInt = ois.readInt();
double readDouble = ois.readDouble();
String str = ois.readUTF();
Date date = (Date) ois.readObject();
System.out.println(readInt);
System.out.println(readDouble);
System.out.println(str);
System.out.println(date);
//3.关闭资源
ois.close();
}
}
1.3 利用对象输出流 向文件写入自定义对象ObjectOutputStream
public class Test03 {
/**
* 知识点:利用对象输出流 向文件写入自定义对象
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
//1.创建流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("io.txt"));
//2.写入自定义对象
oos.writeObject(new User("1445584980", "123123"));
oos.writeObject(new User("1534534534", "111222"));
oos.writeObject(new User("5345356683", "123456"));
oos.writeObject(null);
//3.关闭资源
oos.close();
}
}
public class User implements Serializable{//要实现序列化接口(Serializable)
private static final long serialVersionUID = 4907921883130742331L;
private String username;
private transient String password;
//无参构造,有参构造,get,set方法省略
1.4 利用对象输入流 读取文件中的自定义对象ObjectInputStream
public class Test04 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//1.创建流对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("io.txt"));
//2.读取自定义对象
User user;
while((user = (User)ois.readObject()) != null){
System.out.println(user);
}
//3.关闭资源
ois.close();
}
}
2.内存流
2.1 内存输出流ByteArrayOutputStream
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//关闭资源(内存流是程序到内存的通道,是关闭不掉的)
//baos.close();
//2.写入数据 -- 将数据写入到baos对象中的byte数组里
baos.write("123abc木头人".getBytes());
//获取流对象里的数据
System.out.println(new String(baos.toByteArray()));
System.out.println(baos.toString());
}
}
2.2 内存输入流ByteArrayInputStream
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象
ByteArrayInputStream bais = new ByteArrayInputStream("123abc木头人".getBytes());
//关闭资源(内存流是程序到内存的通道,是关闭不掉的)
//bais.close();
//2.读取数据
byte[] bs = new byte[1024];
int len;
while((len = bais.read(bs)) != -1){
System.out.println(new String(bs, 0,len));
}
}
}
3.打印流
3.1 字节打印流PrintStream
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象
//PrintStream ps = new PrintStream("io.txt");
//1.创建流对象(字节流 -> 字节打印流)
//PrintStream ps = new PrintStream(new FileOutputStream("io.txt"));
//1.创建流对象(字节流 -> 字节打印流) + 在末尾追加
PrintStream ps = new PrintStream(new FileOutputStream("io.txt",true));
//2.写入数据
ps.write("好好学习".getBytes());
//3.关闭资源
ps.close();
}
}
3.2 字符打印流PrintWriter
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象
//PrintWriter pw = new PrintWriter("io.txt");
//1.创建流对象(字节流 -> 字节打印流)
//PrintWriter pw = new PrintWriter(new FileOutputStream("io.txt"));
//1.创建流对象(字节流 -> 字节打印流) + 在末尾追加
//PrintWriter pw = new PrintWriter(new FileOutputStream("io.txt",true));
//1.创建流对象(字符流 -> 字符打印流)
//PrintWriter pw = new PrintWriter(new FileWriter("io.txt"));
//1.创建流对象(字符流 -> 字符打印流) + 在末尾追加
//PrintWriter pw = new PrintWriter(new FileWriter("io.txt",true));
//1.创建流对象(设置编码格式 + 在末尾追加 + 考虑到效率)
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("io.txt",true), "GBK")));
//2.写入数据
pw.write("好好学习");
//3.关闭资源
pw.close();
}
}
4.扩展:重定向
4.1 System.in
public class Test03 {
public static void main(String[] args) throws FileNotFoundException {
//重定向:重新定义系统标准输入流的方向(文件->程序)
System.setIn(new FileInputStream("io.txt"));
InputStream in = System.in;
Scanner scan = new Scanner(in);
String str = scan.next();
System.out.println(str);
scan.close();
}
}
4.2 System.out
public class Test04 {
public static void main(String[] args) throws FileNotFoundException {
//重定向:重新定义系统标准输出流的方向(程序->文件)
System.setOut(new PrintStream(new FileOutputStream("io.txt",true)));
PrintStream ps = System.out;
ps.println("好好学习");
}
}
4.3 System.err
public class Test05 {
public static void main(String[] args) throws FileNotFoundException {
//重定向:重新定义系统标准错误输出流的方向(程序->文件)
System.setErr(new PrintStream(new FileOutputStream("io.txt",true)));
PrintStream ps = System.err;
ps.println("好好学习");
}
}
5.随机访问流
5.1 利用随机访问流 向文件写入数据 RandomAccessFile
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象
RandomAccessFile w = new RandomAccessFile("io.txt", "rw");
//2.写入数据
w.write("123abc木头人".getBytes());
//3.关闭资源
w.close();
}
}
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象
File file = new File("io.txt");
RandomAccessFile w = new RandomAccessFile(file, "rw");
//设置指针的位置
w.seek(file.length());
//2.写入数据
w.write("123abc木头人".getBytes());
//3.关闭资源
w.close();
}
}
5.2 利用随机访问流 读取文件里的数据 RandomAccessFile
public class Test03 {
public static void main(String[] args) throws IOException {
//1.创建流对象
RandomAccessFile r = new RandomAccessFile("io.txt", "r");
//2.读取数据
byte[] bs = new byte[1024];
int len;
while((len = r.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
//3.关闭资源
r.close();
}
}
5.2 利用随机访问流 读取文件里的数据 RandomAccessFile
public class Test04 {
public static void main(String[] args) throws IOException {
//1.创建流对象
RandomAccessFile r = new RandomAccessFile("io.txt", "r");
//设置指针的位置
r.seek(3);
//2.读取数据
byte[] bs = new byte[1024];
int len;
while((len = r.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
//3.关闭资源
r.close();
}
}
5.3 拷贝文件 – 断点续传
public class Copy {
public static void main(String[] args) throws IOException {
RandomAccessFile r = new RandomAccessFile("视频.mp4", "r");
File targetFile = new File("copy.mp4");
RandomAccessFile w = new RandomAccessFile(targetFile, "rw");
//设置指针
long fileLength = targetFile.length();
r.seek(fileLength);
w.seek(fileLength);
byte[] bs = new byte[1024];
int len;
while((len = r.read(bs)) != -1){
w.write(bs, 0, len);
}
r.close();
w.close();
}
}