二、字符流

1、FileReader(输入)
  用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在FileInputStream上构造一个InputStream。
  read():一次读一个字符,指针会指向下一个字符。读到末尾返回-1。
  代码示例:读文件

复制代码
1 // 文件:F:\hello.txt
2 // 内容:helloworld123中国人1
3 public class Main {
4 public static void main(String[] args) {
5 try (FileReader fr = new FileReader(new File(“F:\hello.txt”))😉 {
6 int data;
7 while ((data = fr.read()) != -1) {
8 System.out.print((char) data);
9 }
10 } catch (Exception e) {
11 }
12 }
13 }
14
15 // 结果
16 helloworld123中国人的
复制代码
  read(char[] buff):一次读 buff.length 个字符,返回读取的个数。读到末尾返回-1。
  代码示例:错误的写法

复制代码
1 // 文件:F:\hello.txt
2 // 内容:helloworld123中国人1
3 public class Main {
4 public static void main(String[] args) {
5 try (FileReader fr = new FileReader(new File(“F:\hello.txt”))😉 {
6 char[] buff = new char[5];
7 int len;
8
9 while ((len = fr.read(buff)) != -1) {
10 // 方式一:
11 for (char c : buff) {
12 System.out.print©;
13 }
14
15 // 方式二:
16 // String str = new String(buff);
17 // System.out.print(str);
18 }
19 } catch (Exception e) {
20 }
21 }
22 }
23
24 // 结果.方式一 和 方式二都是
25 helloworld123中国人13中国
复制代码
  代码示例:正确的写法

复制代码
1 // 文件:F:\hello.txt
2 // 内容:helloworld123中国人1
3 public class Main {
4 public static void main(String[] args) {
5 try (FileReader fr = new FileReader(new File(“F:\hello.txt”))😉 {
6 char[] buff = new char[5];
7 int len;
8
9 while ((len = fr.read(buff)) != -1) {
10 // 方式一:
11 for (int i = 0; i < len; i++) {
12 System.out.print(buff[i]);
13 }
14
15 // 方式二:从 buff 的下标 0 开始取字符,取 len 个
16 // String str = new String(buff, 0, len);
17 // System.out.print(str);
18 }
19 } catch (Exception e) {
20 }
21 }
22 }
23
24 // 结果
25 helloworld123中国人1
复制代码
  深刻理解 read(char[] buff) 方法:一次读进 buff.length 个字符,打印。读取下次的时候,上次读取的字符其实还在字符数组中,所以最后"人1"只覆盖了上一次的前两个字符,使得最后字符数组里是"人 1 3 中 国"。

2、FileWriter(输出)
  构造方法必须明确被操作的文件,若指定目录下不存在,会被创建;若存在,会被覆盖。相关API如下:

new FileWriter(String fileName):在指定目录下创建
  new FileWirter(String fileName, boolean append):是否为追加数据
  void write(String int):重载,将字符串写入到流中
  void write(char[] buff):写入字符数组
  void wirte(char[] buff, int off, int len):写入字符数组的某一部分
  void flush():刷新流,流可以继续使用
  void close():先刷新流,关闭流,流不可以继续使用

注:通过write()写入换行用 \r\n,对应的字节为 13 10

代码示例:写文件

复制代码
1 public class Main {
2 public static void main(String[] args) {
3 // 默认不追加,覆盖原有的文件内容
4 try (FileWriter fw = new FileWriter(new File(“F:\hello.txt”))😉 {
5
6 // 表示在原有文件内容的基础上追加
7 // FileWriter fw = new FileWriter(new File(“F:\hello.txt”), true);
8 fw.write(“我有a dream!\n”);
9 fw.write(“you need to have a dream!”);
10
11 // fw.flush();
12 } catch (Exception e) {
13 }
14 }
15 }
复制代码
3、复制文件
  代码示例:字符流复制文本文件

复制代码
1 public class Main {
2 public static void main(String[] args) {
3 try (FileReader fr = new FileReader(new File(“F:\hello.txt”));
4 FileWriter fw = new FileWriter(new File(“F:\hello_copy.txt”))😉 {
5
6 char[] buff = new char[5];
7 int len;
8
9 // 一次读出len个字符到buff.
10 while ((len = fr.read(buff)) != -1) {
11 fw.write(buff, 0, len);
12 fw.flush();
13 }
14 } catch (Exception e) {
15 }
16 }
17 }
复制代码
  注:不能使用字符流来处理图片,视频等字节数据。

返回顶部
三、字节流
1、FileInputStream(输入)
  基本用法和字符流一样。
  代码示例:读文件,可能出现乱码。

复制代码
1 // 文件:F:\hello.txt
2 // 内容:helloworld123中国人1
3 public class Main {
4 public static void main(String[] args) {
5 try (FileInputStream fis = new FileInputStream(new File(“F:\hello.txt”))😉 {
6 // fis.available():返回文件字节数
7 byte[] buffer = new byte[5];
8 int len;
9
10 // 按字节来读取
11 while ((len = fis.read(buffer)) != -1) {
12 String str = new String(buffer, 0, len);
13 System.out.print(str);
14 }
15 } catch (Exception e) {
16
17 }
18 }
19 }
20
21 // 结果,有乱码.原因就是一个汉字占两字节,被分割了.
22 helloworld123��国���1
复制代码
2、FileOutputStream(输出)
  基本用法和字符流一样,不同于在写入时不需要flush()。

void write(int b):一次写入一个字节
  void write(byte[] b):写入一个字节数组
  void write(byte[] b, int off, int len):写入一个字节数组,off开始,len字节数

代码示例:写文件

复制代码
1 public class Main {
2 public static void main(String[] args) {
3 try (FileOutputStream fos = new FileOutputStream(new File(“F:\hello.txt”))😉 {
4
5 fos.write(“我有a dream!\n”.getBytes());
6 fos.write(“you need to have a dream!”.getBytes());
7
8 } catch (Exception e) {
9 }
10 }
11 }
12
13 // 结果,有乱码.
14 鎴戞湁a dream!
15 you need to have a dream!
复制代码
  总结:因为一个汉字占两个字节。所以,
  字符流,适用于读写文本文件。不适用于字节流,容易出现乱码。
  字节流,适用于读写二进制文件,比如图片,音频,视频等。

3、复制文件
  代码示例:用字节流处理非文本文件(图片,视频等)。字节流复制图片。

复制代码
1 // 文件:F:\hello.PNG
2 public class Main {
3 public static void main(String[] args) {
4 try (FileInputStream fis = new FileInputStream(new File(“F:\hello.PNG”));
5 FileOutputStream fos = new FileOutputStream(new File(“F:\hello_1.PNG”))😉 {
6
7 byte[] buffer = new byte[5];
8 int len;
9
10 // 一次性读一个 len 个字节到buffer. len <= buffer
11 while ((len = fis.read(buffer)) != -1) {
12 fos.write(buffer, 0, len);
13 }
14 } catch (Exception e) {
15 }
16 }
17 }
18
19 // 此代码用于复制 3.64G 的文件花费 56.574s
亚马逊测评 www.yisuping.cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值