十个Demo进行讲解Java中IO流的常用操作

网友投稿 581 2022-05-30

好久不见的IO流

对IO流的学习,我记得还是初学Java基础的时候,后来找工作过程中经常看到有些招聘信息中写到熟悉IO流,现在想想IO流,真的是一脸懵逼,不说这么多废话了,IO流这次好好整理一下。

说说IO流的类别

在说流的类别之前,先说说什么是流,流其实就是对输入输出设备的抽象,可以把输入输出流理解为是一个通道,输入输出是相对程序而言的,如果是输出流,也就是往文件中写文件,而输入流,则是从文件中读取文件。从三个方面对IO流进行总结,一、字节流(一般都是xxxStream),二、字符流(xxxRead、xxxWrite),三、缓冲流。其实也可以简单的分为两类,分别是输入流和输出流。

聊聊文件

在讲解IO流之前,有必要说说文件的操作,毕竟IO操作大部分也就是文件嘛。好了先来看看JDK-API文档吧,首先看看构造方法。

有了构造方法,我们可以通过构造方法创建对象,然后操作一波文件,创建对象之后,点一下,好家伙,很多可用方法,其实常用的不多。上号,开!

先来看看创建文件的方法吧

File file = new File("a.txt"); boolean newFile = file.createNewFile();

1

2

还能创建目录呢,不服来战

File file1 = new File("test"); file1.mkdir();

1

2

还有判断文件是否存在的方法也很常用

boolean exists = file.exists();

1

还有好多操作,自己可以试试,有了目录文件了,是不是该往里面写点东西了,来吧~

字节输入流

输入流,是相对于程序而言的,也就是从文件中读取文件,先看构造方法。

// 创建字节输入流对象 FileInputStream fis1 = new FileInputStream("a.txt"); // 用单字节进行读取 int x = 0; while ((x = fis1.read()) != -1) { System.out.println((char) x); }

1

2

3

4

5

6

7

8

这样一波操作之后,他会把a.txt文件里的内容读取出来,但是是单字节的读的,单字节的效率还是比较低的,一般根据实际情况来进行自定义字节数读取,下面通过自定义字节搞一波。

// 创建字节输入流对象 FileInputStream fis2 = new FileInputStream("a.txt"); // 用字节数组进行读取 byte[] b = new byte[1024]; int len = 0; while ((len = fis2.read(b)) != -1) { System.out.print(new String(b, 0, len)); }

1

2

3

4

5

6

7

8

字节输出流

输出流,可以将文件写入到文件中,一般日志文件写的比较多。

//创建字节输出流对象 FileOutputStream fos = new FileOutputStream("a.txt"); //调用write()方法 fos.write("hello".getBytes());

1

2

3

4

这样一波操作之后,就可以把“hello”字符串转化为字节,然后写入到文件中,也可以读取a.txt文件中的内容,写入到b.txt文件中

InputStream in = new FileInputStream("a.txt"); OutputStream os = new FileOutputStream("b.txt"); byte[] bytes = new byte[2]; int n; while ((n = in.read(bytes)) != -1) { os.write(bytes, 0, n); }

1

2

3

4

5

6

7

字符输入流

一个汉字大约占两个字节,而当用字节流处理的时候,可能会出现乱码的情况。字符输入流FileRead,先来体验一下,老规矩,先来构造方法。

Reader r = new FileReader("a.txt"); int n; char[] chars = new char[2]; while ((n = r.read(chars)) != -1) { String s = new String(chars,0,n); }

1

2

3

4

5

6

其实跟字节流差不多,只是这里用char[]字符数组来进行操作了。

字符输出流

直接上构造方法

字符写入的操作,还以读取a.txt文件中的内容到b.txt文件中

Reader r = new FileReader("a.txt"); Writer w = new FileWriter("b.txt"); int n; char[] chars = new char[3]; while ((n = r.read(chars)) != -1) { w.write(chars,0,n); }

1

2

3

4

5

6

7

字节缓冲输入流

老规矩,先看构造方法

可以看出,要传入一个流的参数。

BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("a.txt")); // 用字节数组进行读取 byte[] b = new byte[1024]; int len = 0; while ((len = bis2.read(b)) != -1) { System.out.print(new String(b, 0, len)); }

1

2

3

4

5

6

7

字节缓冲输出流

字节缓冲输出流跟输入流差不多,可以类比着看。

同样的以读取a.txt的文件到b.txt为例

InputStream inputStream = new FileInputStream("a.txt"); BufferedInputStream bis = new BufferedInputStream(inputStream); OutputStream outputStream = new FileOutputStream("b.txt"); BufferedOutputStream bos = new BufferedOutputStream(outputStream); byte[] b = new byte[1024]; int n = 0; while (bis.read(b) != -1) { bos.write(b); }

1

2

3

4

5

6

7

8

十个Demo进行讲解Java中IO流的常用操作

9

字符缓冲输入流

字符缓冲输入流的参数是字符流

Reader in = new FileReader("a.txt"); BufferedReader bufferedReader = new BufferedReader(in); String str; while ((str = bufferedReader.readLine()) != null) { System.out.println(str); }

1

2

3

4

5

6

字符缓冲输出流

同样的,以读取a.txt文件的内容到b.txt为例

Reader in = new FileReader("a.txt"); Writer out = new FileWriter("b.txt"); BufferedReader bufferedReader = new BufferedReader(in); BufferedWriter bufferedWriter = new BufferedWriter(out); String str; while ((str = bufferedReader.readLine()) != null) { bufferedWriter.write(str); bufferedWriter.newLine(); }

1

2

3

4

5

6

7

8

9

10

流的关闭

上面的demo中,为了让代码简介减少重复,就没有对流进行关闭操作,这里统一说明一下,流在使用后,要进行close()关闭。

Java 数据结构

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序开通cms可视化网页后台
下一篇:华为云DevCloud携手图扑,深耕工业物联网领域
相关文章