600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Java入门学习第十七天——字节流读写文件 字节缓冲流拷贝文件

Java入门学习第十七天——字节流读写文件 字节缓冲流拷贝文件

时间:2023-06-20 00:36:02

相关推荐

Java入门学习第十七天——字节流读写文件 字节缓冲流拷贝文件

字节流读写文件

字节流拷贝文件—按单个字节读写

FileInputStream:普通的字节输入流,用来读取数据的

构造方法:

​ public FileInputStream(String pathname);

成员方法:

​ public int read();

单个:一次读取一个字节,并返回读取到的内容,读不到返回-1

数组:一次读取一个字节数组,将读取到的内容存入到数组中,并返回读取到的有效字节数,读不到则返回-1

FileOutputStream:普通的字节输出流,用来写数据

构造方法:

​ public FileOutputStream(String pathname);

成员方法:

​ public void write(int len);

单个:一次写入一个字节

数组:一次写入一个指定的字节数组

步骤:

创建字节流读文件对象:

InputStream is = new FileInputStream(“1.jpg”);

创建字节流写文件对象:

OutputStream os = new FileOutputStream(“D:\2.jpg”);

使用while循环读写数据:

int b;

while (b = is.read() != -1) {

​ os.write(b);

}

异常处理:

throwsIOException

关闭资源:

is.close();

os.close();

自行在相关文件夹下放入图片

package io.demo;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class StreamDemo {public static void main(String[] args) throws IOException {// 需求:通过普通的字节流,一次读写一个字节的方式,将a.jpg复制到b.jpg中// 1.创建字节输入流,关联数据源文件FileInputStream is = new FileInputStream("lib/1.jpg");// 2.创建字节输出流,关联目的地文件FileOutputStream os = new FileOutputStream("lib/2.jpg");// 3.定义变量,记录读取到的内容int i;// 4.循环读取,只要条件满足就一直读取,并将读取到的内容渎职给变量while ((i = is.read()) != -1) {// 5.将读取到的内容写入目的地文件中os.write(i);}// 6.释放资源is.close();os.close();}}

字节流拷贝文件—按字节数组读写

创建字节流读文件对象:

InputStream is = new FileInputStream(“1.jpg”);

创建字节流写文件对象:

OutputStream os = new FileOutputStream(“D:\2.jpg”);

异常处理:

throwsIOException

关闭资源:

is.close();

os.close();

定义字节数组,每次读取2048个字节:

byte[] b = new byte[2048];

使用while循环读写数据:

int i;

while (i = is.read() != -1) {

​ os.write(b,0,i);

}

package io.demo;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class StreamDemo2 {public static void main(String[] args) throws IOException {// 需求:通过普通的字节流,一次读写一个字节数组的方式,将a.jpg复制到b.jpg中// 1.创建字节输入流对象,关联数据源文件FileInputStream is = new FileInputStream("lib/1.jpg");// 2.创建字节输出流对象,关联目的地文件FileOutputStream os = new FileOutputStream("lib/3.jpg");// 3.定义变量,接收读取到的内容byte[] b = new byte[1024]; //[]里面写多少都行,最好是写1024的整数倍// 用来记录读取到的有效字节数int len;// 4.循环读取,只要条件满足,就一直读取,并将读取到的内容(有效的字节数)赋值给变量while ((len = is.read(b)) != -1) {// 5.将读取到的数据写入目的地文件os.write(b,0,len);}// 6.释放文件is.close();os.close();}}

字节缓冲流拷贝文件的标准代码

BufferedInputStream:字节缓冲输入流(也叫高效字节输入流),用来读数据的;

构造方法:

​ public BufferedInputStream(InputStream is);

成员方法:

​ public int read();

单个:一次读取一个一个字节,并返回读取到的内容,读不到返回-1;

BufferedOutputStream:字节缓冲输出流(也叫高效字节输出流),用来写数据的;

构造方法:

​ public BufferedOutputStream(OutputStream os);

成员方法:

​ public void write(int len);

单个:一次写入一个字节;

**特点:**字节缓冲流有自己的缓冲区,大小为8192个字节,也就是8KB

步骤:

创建字节缓冲流读文件对象:

​ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“1.jpg”));

创建字节缓冲流写文件对象:

​ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“2.jpg”));

异常处理:

throwsIOException

关闭资源:

is.close();

os.close();

使用while循环读写数据:

int i;

while (i = bis.read() != -1) {

​ os.write(i);

}

package io.demo;import java.io.*;//总结:// 拷贝纯文本文件使用字符流,拷贝其他(图片,视频,音频)使用字节流public class StreamDemo3 {public static void main(String[] args) throws IOException {//需求:通过字节缓冲流,将1.jpg复制到2.jpg中// 1.创建字节输入流对象,关联数据源文件// 分写// FileInputStream is = new FileInputStream("lib/1.jpg");// BufferedInputStream bis = new BufferedInputStream(is);// 合并BufferedInputStream bis = new BufferedInputStream(new FileInputStream("lib/1.txt"));// 2.创建字节输出流对象,关联目的地文件// 分写// FileOutputStream os = new FileOutputStream("lib/2.jpg");// BufferedOutputStream bos = new BufferedOutputStream(os);// 合并BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("lib/2.txt"));// 3.定义变量,用来存放读取到的数据int len;// 4.循环读取,只要条件满足就一直读取,并将读取到的内容赋值给变量while ((len = bis.read()) != -1) {//底层是按照字节数组来操作的// 5.将读取到的内容写入目的地文件中bos.write(len);}// 6.释放资源bis.close();bos.close();}}

**总结:**拷贝纯文本文件使用字符流,拷贝其他(图片,视频,音频)使用字节流。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。