java高级流_Java中的高级I/O流-缓冲流、数据流以及对象流

Java中的高级I/O流-缓冲流、数据流以及对象流

前言:通过前面的学习,已经学完了Java中的基本流;Java中的流有字节流和字符流两大类,而每一种流都有对应的输入和输出流;

1、字节流

1.1字节输入流-主要是:FileInputStream

1.2字节输出流-主要是:FileOutputStream

2、字符流

2.1字符输入流-主要是:FileReader

2.2字符输出流-主要是:FileWriter

无论是字节或者字符输入流都有三种主要的read()方法,分别是读单字节/字符read()方法   利用字节/字符数组进行读 read(byte [ ] bytes) 为了防止最后一次读入有误,我们一般会指定每次字节/字符数组的读入read(byte [ ] bytes,int start int len)

那么相对应的无论字节或者是字符输出流都有一种和输入流相对应的写方法,所以写方法也主要有三种,看你是怎么读入进来的,我就怎么写出去;

总结起来,其实就学了两对流(一对字节流,一对字符流),三种读写的方法(单字节/字符读写 字节/字符数组读写  指定位置的字节/字符的读写)

Java中除了基本的I/O流,还有高级的I/O流,那就是缓冲流和包装流(包括数据流和对象流)

一、缓冲流

42eb5e1363c71c0a628b16bdb25f8017.png

为什么要有缓冲流?

比如说,家里盖房子,有一堆砖头要搬在工地100米外,单字节的读取就好比你一个人每次搬一块砖头,从堆砖头的地方搬到工地,这样可定很费时间,然后好的方法就是多叫几个小伙伴帮你一起搬砖头,这样因为人多了,每次可以搬十块砖头,但效率还是很低,这就好比我们的字节/字符数组读写操作;然而聪明的人类会用小推车,每次先搬砖头搬到小车上,再利用小推车运到工地上去,这样你或者你的小伙伴们再从小推车上取砖头是不是方便多了呀!这样效率就会大大提高,缓冲流就好比我们的小推车;给砖头暂时提供一个可存放的空间;

注意:缓冲流属于包装流,只能对已有的流进行封装,不能直接关联文件进行操作

二、字节缓冲流

d848e9de1eeb07eca79e2de5da67ed23.png

9224f3495073ab2ded34a2d9ad01ee06.png

三、字符缓冲流

e4abad74549b1794bf37708913d2e7bf.png

98c32bcb92260c8d07e6b3583505e4ec.png

e37ccda0987f17525a300f81df69b12d.png

四、字节和字符缓冲流的使用:使用缓冲流进行文件的拷贝

源代码:

package com.huaxin.zhou;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.InputStreamReader;

public class IOcopy {

public static void main(String[] args) {

IOcopy icopy = new IOcopy();

icopy.copy3();

icopy.copy4();

}

public void copy3() {

try {

//创建字节输入流对象

FileInputStream fis

= new FileInputStream("C:\\Users\\Administrator\\Desktop\\my.doc");

//创建字节输出流对象

FileOutputStream fos

= new FileOutputStream("C:\\Users\\Administrator\\Desktop\\mycopy.doc");

//利用字节缓冲流包装字节输入和输出流

BufferedInputStream bis = new BufferedInputStream(fis,1024);

BufferedOutputStream bos = new BufferedOutputStream(fos);

//从小推车上一个一个取砖头下来

//int value =bis.read();

//

//while( value!=-1){

//bos.write(value);

//bos.flush();

//value =bis.read();

//}

//很多小伙伴一起从小推车上取砖头

byte [] bytes = new byte[10];

int value =bis.read(bytes);

while( value!=-1){

bos.write(bytes,0,value);

bos.flush();

value =bis.read(bytes);

}

//关闭流

bis.close();

bos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public void copy4() {

//构建字符输入流对象

FileReader fr;

try {

fr = new FileReader("C:\\Users\\Administrator\\Desktop\\1.txt");

//构建字符输出流对象

FileWriter fw =

new FileWriter("C:\\Users\\Administrator\\Desktop\\11.txt");

//字符缓冲流对字符输入输出流进行包装

BufferedReader br = new BufferedReader(fr);

BufferedWriter bw = new BufferedWriter(fw);

//单字节从小推车上取砖头

//int value =br.read();

//while(value!=-1){

//bw.write(value);

//bw.flush();

//value =br.read();

//}

//很多小伙伴们一起取砖头

char [] chars = new char[10];

int value =br.read(chars);

while(value!=-1){

bw.write(chars,0,value);

bw.flush();

value=br.read(chars);

}

//利用readLine方法每次读取一行内容

//String value=br.readLine();

//

//输出单个字符

//while(value!=null){

//bw.write(value);

//bw.flush();

//value=br.readLine();

//}

//关闭流

br.close();

bw.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

五、数据流

分析下面程序的运行结果:

package com.huaxin.IORW;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import sun.reflect.FieldInfo;

public class IOTest {

public static void main(String[] args) {

int a=200;

int b=1000;

try {

FileOutputStream fos =

new FileOutputStream("C:\\Users\\Administrator\\Desktop\\2.txt");

fos.write(a);

fos.write(b);

fos.close();

FileInputStream fis = new

FileInputStream("C:\\Users\\Administrator\\Desktop\\2.txt");

int value1=fis.read();

System.out.println(value1);

int value2=fis.read();

System.out.println(value2);

} catch (Exception e) {

e.printStackTrace();

}

}

}

运行结果:

a3b971d58247eda4518883bca33f7285.png

为什么后面写入的1000读出来变成了232了呢?

因为我们是读出一个字节,一个字节是八位,所以能读出的最大整数值是255;那么怎么把1000读取出来呢?这个时候就要用到数据流了!

a6e745bae4506fca815fa4711dc22740.png

1c928f3fd90f1cd92ff251dd9fada4b2.png

数据流的写方法,同样有读方法,参看Java的API 文档

5841c7be67b9c0e449e50d2355d13d8f.png

六、对象流

对象流就比较好玩了,当我们在程序中定义了一个学生类,并且想把这个学生类的实例用文件保存起来,有需要的时候再读取这个学生的信息;对象流就是解决这样的问题,可以把类类型的数据永久的保存起来;

9aa06603bbbf295166b307d6850c3984.png

724d2658963af69bf57cb2a427a7148b.png

d3f5d85b538b3ff8dc625934025328f2.png

对象流的使用:

Student类:

package com.huaxin.IORW;

import java.io.Serializable;

public class Student implements Serializable{

String name;

int age;

public Student(){

}

public Student(String name,int age){

this.name=name;

this.age=age;

}

}

测试类:

package com.huaxin.IORW;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class ObjectTest {

public static void main(String[] args) {

//创建两个学生对象

Student stu1 = new Student("张三",20);

Student stu2 = new Student("流浪洲",21);

try {

//构建输出流

FileOutputStream fos = new

FileOutputStream("C:\\Users\\Administrator\\Desktop\\3.txt");

//利用对象输出流包装输出流

ObjectOutputStream oos =new ObjectOutputStream(fos);

//写出对象

oos.writeObject(stu1);

oos.writeObject(stu2);

oos.close();

//创建输入流

FileInputStream fis = new

FileInputStream("C:\\Users\\Administrator\\Desktop\\3.txt");

//对象输入流包装输入流

ObjectInputStream ois = new ObjectInputStream(fis);

//读取对象,并强制转型为Student类型

Student stu11=(Student)ois.readObject();

System.out.println(stu11.name+":"+stu11.age);

Student stu22=(Student)ois.readObject();

System.out.println(stu22.name+":"+stu22.age);

} catch (Exception e) {

e.printStackTrace();

}

}

}

运行结果:

f3a40c9404003e0d761808c92ccfd539.png

使用特殊协议保存起来的对象

deadf9ca11a53b02bbd1fe9ed61cd85a.png

需要注意的是:进行序列化时,切记所有相关的类要时间Serializable接口

总结:

1、Java中的高级流主要有缓冲流(包括字节和字符缓冲流)、数据流、对象流

2、弄明白其中的原理,这些流都解决了什么样的问题?那个缓冲里那里,得好好理解,一开始我以为我理解了,其实并没有,想明白利用字节数组进行读以及缓冲流进行读的区别;

3、以前对对象流觉得很不好理解,现在学明白了,才发现对象流原来大有用处呀!

4、学无止境,一起共勉!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 使用 I/O 复制文件可以使用如下的代码: ``` import java.io.*; public class CopyFile { public static void main(String[] args) { // 要复制的文件的路径 String srcFile = "/path/to/src/file.txt"; // 复制后的文件的路径 String destFile = "/path/to/dest/file.txt"; try (InputStream in = new FileInputStream(srcFile); OutputStream out = new FileOutputStream(destFile)) { // 缓冲区大小 byte[] buffer = new byte[1024]; int length; // 循环读取文件内容并写入到新文件 while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码使用了 try-with-resources 语句,这样在结束后会自动关闭。如果不使用 try-with-resources 语句,需要在 finally 块手动关闭。 需要注意的是,上面的代码只能复制纯文本文件。如果要复制二进制文件,需要使用字节(InputStream 和 OutputStream)而不是字符(Reader 和 Writer)。 ### 回答2: 文件复制通过使用Java的I/O可以轻松地实现。下面是一个简单的示例,演示了如何使用I/O来实现文件复制。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopy { public static void main(String[] args) { String sourceFilePath = "source.txt"; // 源文件路径 String targetFilePath = "target.txt"; // 目标文件路径 try { // 创建输入和输出 FileInputStream input = new FileInputStream(sourceFilePath); FileOutputStream output = new FileOutputStream(targetFilePath); // 创建一个缓冲区,用于存储读取/写入的数据 byte[] buffer = new byte[1024]; int bytesRead; // 读取源文件的数据,并将其写入目标文件 while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } // 关闭 input.close(); output.close(); System.out.println("文件复制完成!"); } catch (IOException e) { System.out.println("发生错误:" + e.getMessage()); } } } ``` 上述代码,我们首先指定源文件和目标文件的路径。然后,我们使用`FileInputStream`和`FileOutputStream`类创建输入和输出。我们还创建了一个缓冲区,用于存储读取和写入的数据。 在`while`循环,我们使用`read()`方法从输入读取数据,并使用`write()`方法将读取的数据写入输出。我们还使用`bytesRead`变量来记录实际读取的字节数,以便正确写入到输出。 最后,我们在`IOException`捕获任何可能发生的错误,并将错误消息输出到控制台。如果没有错误发生,则输出"文件复制完成!"的消息。 通过运行上述代码,我们可以将一个文件的内容复制到另一个文件。请确保源文件存在,并且目标文件不存在,以避免发生意外的覆盖。 ### 回答3: Java使用I/O完成文件复制非常简单。我们可以使用字节或字符来实现文件复制。 使用字节实现文件复制时,首先需要创建一个文件输入和一个文件输出。然后,通过循环从文件输入读取字节,并将其写入文件输出,实现文件复制。最后记得关闭输入和输出。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); // 定义一个字节数组来存储读取的字节数据 byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭输入和输出 inputStream.close(); outputStream.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符实现文件复制时,操作步骤与字节类似。只是在创建时需要使用Reader和Writer类,同时要保证源文件和目标文件的编码类型一致。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileReader reader = new FileReader(inputFilePath); FileWriter writer = new FileWriter(outputFilePath); // 定义一个字符数组来存储读取的字符数据 char[] buffer = new char[1024]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } // 关闭输入和输出 reader.close(); writer.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 无论使用字节还是字符Java的I/O都提供了方便的方法来实现文件复制。通过以上的代码示例,我们可以轻松地完成文件复制操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值