一、字符流
字符输入流
java.io.Reader 抽象类是表示⽤于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输⼊流的基本共性功能⽅法。
public void close() :关闭此流并释放与此流相关联的任何系统资源。
public int read() : 从输⼊流读取⼀个字符。
public int read(char[] cbuf) : 从输⼊流中读取⼀些字符,并将它们存储到字符数组cbuf中
FileReader类
1
2
3FileReader(File file) : 创建⼀个新的 FileReader ,给定要读取的File对象。 FileReader(String fileName) : 创建⼀个新的 FileReader ,给定要读取的⽂件的名称。
构造时使⽤系统默认的字符编码和默认字节缓冲.
- 字符编码:字节与字符的对应规则。Windows系统的中⽂编码默认是GBK编码表。idea中UTF-8
2.字节缓冲区:⼀个字节数组,⽤来临时存储字节数据
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public static void main(String[] args) throws IOException { // 使⽤File对象创建流对象 File file = new File("a.txt"); //FileReader fr = new FileReader(file); // 使⽤⽂件名称创建流对象 //FileReader fr = new FileReader("b.txt"); // 使⽤⽂件名称创建流对象 FileReader fr = new FileReader("read.txt"); // 定义变量,保存有效字符个数 int len ; // 定义字符数组,作为装字符数据的容器 char[] cbuf = new char[2]; // 循环读取 while ((len = fr.read(cbuf))!=-1) { System.out.println(new String(cbuf,0,len)); } // 关闭资源 fr.close(); }
字符输出流
java.io.Writer 抽象类是表示⽤于写出字符流的所有类的超类,将指定的字符信息写出到⽬的地。它定义了字符输出流的基本共性功能⽅法。
void write(int c) 写⼊单个字符
void write(char[] cbuf) 写⼊字符数组。
abstract void write(char[] cbuf, int off, int len) 写⼊字符数组的某⼀部分,off数组的开始索引,len写的字符个数。
void write(String str) 写⼊字符串。
void write(String str, int off, int len) 写⼊字符串的某⼀部分,off字符串的开始索引,len写的字符个数。
void flush() 刷新该流的缓冲。
void close() 关闭此流,但要先刷新它。
FileWriter类
FileWriter(File file) : 创建⼀个新的 FileWriter,给定要读取的File对象。
FileWriter(String fileName) : 创建⼀个新的 FileWriter,给定要读取的⽂件的名称.
1、写出字符: write(int b) ⽅法,每次可以写出⼀个字符数据
2、写出字符数组 : write(char[] cbuf) 和 write(char[] cbuf, int off, int
len) ,每次可以写出字符数组中的数据,⽤法类似FileOutputStream
3、写出字符串: write(String str) 和 write(String str, int off, int len)
,每次可以写出字符串中的数据,更为⽅便。
因为内置缓冲区的原因,如果不关闭输出流,⽆法写出字符到⽂件中。但是关闭的流对象,是⽆法继续写出数据的。如果我们既想写出数据,⼜想继续使⽤流,就需要flush ⽅法了。
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50public static void main(String[] args) throws IOException { // 使⽤File对象创建流对象 File file = new File("fw.txt"); // FileWriter fw = new FileWriter(file); // 使⽤⽂件名称创建流对象 // FileWriter fw = new FileWriter("fw.txt"); // 使⽤⽂件名称创建流对象 FileWriter fw = new FileWriter("fw.txt"); // 写出数据 fw.write(97); // 写出第1个字符 fw.write('b'); // 写出第2个字符 fw.write('C'); // 写出第3个字符 fw.write(30000); // 写出第4个字符,中⽂编码表中30000对应⼀个汉字。 /*【注意】关闭资源时,与FileOutputStream不同。 如果不关闭,数据只是保存到缓冲区,并未保存到⽂件。 */ // fw.close(); // 使⽤⽂件名称创建流对象 FileWriter fw1 = new FileWriter("fw.txt"); // 写出数据,通过flush fw1.write('刷'); // 写出第1个字符 fw1.flush(); fw1.write('新'); // 继续写出第2个字符,写出成功 fw1.flush(); // 写出数据,通过close fw1.write('关'); // 写出第1个字符 fw1.close(); fw1.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream fw.close(); // 使⽤⽂件名称创建流对象 FileWriter fw2 = new FileWriter("fw.txt"); // 字符串转换为字节数组 char[] chars = "你好中国".toCharArray(); // 写出字符数组 fw2.write(chars); // 写出从索引2开始,2个字节。索引2是'中',两个字节,也就是'中国'。 fw2.write(chars,2,2); //中国 fw2.close(); // 使⽤⽂件名称创建流对象 FileWriter fw3 = new FileWriter("fw.txt"); // 字符串 String msg = "你好中国"; // 写出字符数组 fw3.write(msg); //你好中国 // 写出从索引2开始,2个字节。索引2是'中',两个字节,也就是'中国'。 fw3.write(msg,2,2); // 中国 // 关闭资源 fw3.close(); }
二、缓冲流
缓冲流,也叫⾼效流,是对4个基本的 FileXxx 流的增强,所以也是4个流,按照数据类型分类:
字节缓冲流: BufferedInputStream , BufferedOutputStream;
字节缓冲流: BufferedInputStream , BufferedOutputStream
字节缓冲流:
构造方法:
public BufferedInputStream(InputStream in) :创建⼀个新的缓冲输⼊流。
public BufferedOutputStream(OutputStream out) :创建⼀个新的缓冲输出流。
1
2
3
4
5
6
7// 创建字节缓冲输⼊流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt")); // 创建字节缓冲输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
效率问题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// 基本流 public static void main(String[] args) throws IOException { // 记录开始时间 long start = System.currentTimeMillis(); // 创建流对象 try ( FileInputStream fis = new FileInputStream("jdk8.exe"); FileOutputStream fos = new FileOutputStream("copy.exe") ){ // 读写数据 int b; while ((b = fis.read()) != -1) { fos.write(b); } } catch (IOException e) { e.printStackTrace(); } // 记录结束时间 long end = System.currentTimeMillis(); System.out.println("普通流复制时间:"+(end - start)+" 毫秒"); }
缓冲流代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30public static void main(String[] args) throws IOException { // 记录开始时间 long start = System.currentTimeMillis(); // 创建流对象 try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk8.exe")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); ){ // 读写数据 //int b; //while ((b = bis.read()) != -1) { //bos.write(b); //} // 读写数据 int len; byte[] bytes = new byte[8*1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0 , len); } } catch (IOException e) { e.printStackTrace(); } // 记录结束时间 long end = System.currentTimeMillis(); System.out.println("缓冲流复制时间:"+(end - start)+" 毫秒"); }
字符缓冲流
构造⽅法:
public BufferedReader(Reader in) :创建⼀个 新的缓冲输⼊流。
public BufferedWriter(Writer out) : 创建⼀个新的缓冲输出流。
1
2
3
4
5// 创建字符缓冲输⼊流 BufferedReader br = new BufferedReader(new FileReader("br.txt")); // 创建字符缓冲输出流 BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
特有方法:
BufferedReader: public String readLine() : 读⼀⾏⽂字。
BufferedWriter: public void newLine() : 写⼀⾏⾏分隔符,由系统属性定义符号。
示例1:
1
2
3
4
5
6
7
8
9
10
11
12// 创建流对象 BufferedReader br = new BufferedReader(new FileReader("in.txt")); // 定义字符串,保存读取的⼀⾏⽂字 String line = null; // 循环读取,读取到最后返回null while ((line = br.readLine())!=null) { System.out.print(line); System.out.println("------"); } // 释放资源 br.close();
示例2:
1
2
3
4
5
6
7
8
9
10
11
12
13// 创建流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); // 写出数据 bw.write("hello"); // 写出换⾏ bw.newLine(); bw.write("world"); bw.newLine(); bw.write("!"); bw.newLine(); // 释放资源 bw.close();
三、转换流
在IDEA中,使⽤ FileReader 读取项⽬中的⽂本⽂件。由于IDEA的设置,都是默认的 UTF-8编码,所以没有任何问题。但是,当读取Windows系统中创建的⽂本⽂件时,由于Windows系统的默认是GBK编码,就会出现乱码。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13public class ReaderDemo { public static void main(String[] args) throws IOException { FileReader fileReader = new FileReader("E:\File_GBK.txt"); int read; while ((read = fileReader.read()) != -1) { System.out.print((char)read); } fileReader.close(); } } 输出结果: ���
InputStreamReader类
转换流 java.io.InputStreamReader ,是Reader的⼦类,是从字节流到字符流的桥梁。它读取字节,并使⽤指定的字符集将其解码为字符。它的字符集可以由名称指定,也可以接受平台的默认字符集。
构造方法:
InputStreamReader(InputStream in) : 创建⼀个使⽤默认字符集的字符流
InputStreamReader(InputStream in, String charsetName) : 创建⼀个指定字符集的字符流。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21// 定义⽂件路径,⽂件为gbk编码 String FileName = "E:\file_gbk.txt"; // 创建流对象,默认UTF8编码 InputStreamReader isr = new InputStreamReader(new FileInputStream(FileName)); // 创建流对象,指定GBK编码 InputStreamReader isr2 = new InputStreamReader(new FileInputStream(FileName) , "GBK"); // 定义变量,保存字符 int read; // 使⽤默认编码字符流读取,乱码 while ((read = isr.read()) != -1) { System.out.print((char)read); // ��Һ� } isr.close(); // 使⽤指定编码字符流读取,正常解析 while ((read = isr2.read()) != -1) { System.out.print((char)read);// ⼤家好 } isr2.close();
OutputStreamWriter类
转换流 java.io.OutputStreamWriter ,是Writer的⼦类,是从字符流到字节流的桥梁。使⽤指定的字符集将字符编码为字节。它的字符集可以由名称指定,也可以接受平台的默认字符集。
构造方法:
OutputStreamWriter(OutputStream in) : 创建⼀个使⽤默认字符集的字符流。
OutputStreamWriter(OutputStream in, String charsetName) : 创建⼀个指定字符集的字符流。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// 定义⽂件路径 String FileName = "out.txt"; // 创建流对象,默认UTF8编码 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(FileName)); // 写出数据 osw.write("你好"); // 保存为6个字节 osw.close(); // 定义⽂件路径 String FileName2 = "out2.txt"; // 创建流对象,指定GBK编码 OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream(FileName2),"GBK"); // 写出数据 osw2.write("你好");// 保存为4个字节 osw2.close();
最后
以上就是纯真灯泡最近收集整理的关于JAVA-IO流的全部内容,更多相关JAVA-IO流内容请搜索靠谱客的其他文章。
发表评论 取消回复