1.1 IO流概述及其分类
* 1.概念
* IO流用来处理设备之间的数据传输
* Java对数据的操作是通过流的方式
* Java用于操作流的类都在IO包中
* 流按流向分为两种:输入流(读取数据),输出流(写入数据)。
* 流按操作类型分为两种:
* 字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
* 字符流 : 字符流只能操作纯字符数据,比较方便。
* 2.IO流常用父类
* 字节流的抽象父类:
* InputStream
* OutputStream
* 字符流的抽象父类:
* Reader
* Writer
* 3.IO程序书写
* 使用前,导入IO包中的类
* 使用时,进行IO异常处理
* 使用后,释放资源
1.2 FileInputStream
package com.se.file; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo02_FIs { /** * * read()一次读取一个字节 * 1、创建一个文件输入流,关联相应的文件 * 2、定义一个int变量用来接收每次读到的字节 * 3、关闭释放资源 * * @param args */ public static void main(String[] args) { FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream("F:\Demo1\w.txt"); //创建一个文件输入流对象,并关联aaa.txt bis = new BufferedInputStream(fis); int b = -1; //定义变量,记录每次读到的字节 byte[] bytes = new byte[1024]; while ((b = bis.read(bytes)) != -1) { //将每次读到的字节赋值给b并判断是否是-1 String s = new String(bytes, 0, b); System.out.println(s); //打印每一个字符串 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) //关闭流释放资源 fis.close(); if (bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com.jhedu.day22; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo1_FileInputStream { public static void main(String[] args) throws IOException { /** * 从文件当中读取一个字节的数据 * 1. 使用FileInputStream 读取文件 * 2. 调用read方法读取一个自己 * 3. 判断读取的字节是否为-1,如果为-1,就结束循环,否则,继续执行 * 4. 关闭文件,释放资源!!!!!!!!!!!!!! * 注意:需要进行异常处理 */ FileInputStream fis = new FileInputStream("src\com\jhedu\day22\a.txt"); int b; while((b = fis.read()) !=-1){ System.out.println(b); } fis.close(); } protected static void demo() throws IOException { // 相对路径的好处是项目之间不需要考虑修改路的问题 FileInputStream fis = new FileInputStream("src\com\jhedu\day22\a.txt"); /** * 1. throws抛出的是文件找不到的错误 * 2. 读文件的时候也有可能读不成功,同样需要进行异常处理 * 3. FileNotFoundException是IOException的一个子类 * 4. 输出的内容就是GBK编码之,原因GBK编码包含了ASCII码表 * 5. -1 表示文件的结尾,当遇到-1时,表示文件已经读取完毕 */ int x = fis.read(); System.out.println(x); int y = fis.read(); System.out.println(y); int z = fis.read(); System.out.println(z); int d = fis.read(); System.out.println(d); fis.close(); //释放资源 } }
Fileinputstream 相当于一个管子一样,从内存中指到硬盘上的xxx.txt 文件上从文件上读数据。Read fis.read 相当于读到一个字节。然后给x print(x)
Fis.close()关闭流释放资源。
1.3 read()方法返回值为什么是int
* read()方法读取的是一个字节,为什么返回是int,而不是byte
因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,如果每次读取都返回byte,有可能在读到中间的时候遇到111111111,那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,所以在读取的时候用int类型接收,如果11111111会在其前面补上
24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样可以保证整个数据读完,而结束标记的-1就是int类型
1.4 FileOutputStream
* write()一次写出一个字节
FileOutputStream fos = new FileOutputStream("bbb.txt",true); //如果没有bbb.txt,会创建出一个,true表示追加
//fos.write(97); //虽然写出的是一个int数,但是在写出的时候会将前面的24个0去掉,所以写出的一个byte
package com.se.file; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo03_Fos { /** * 输出流FileOutputStream:向文件当中写入数据 * 1. 使用FileOutputStream打开流 * 2. 使用write方法写入数据 * 3. 关闭文件,释放资源 * 4. FileOutPutStream构造方法中,传入append标志为true时,write方法不会覆盖掉原理的内容 * 注意:当目录当中没有相应的文件时,FileOutputStream会自动创建文件 */ public static void main(String[] args) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { // FileOutputStream追加,在文件路径后面添加一个true fos = new FileOutputStream("F:\Demo1\bbb.txt", true); //如果没有bbb.txt,会创建出一个 bos = new BufferedOutputStream(fos); String name = "张三"; fos.write(name.getBytes()); // write()一次写出一个字节 fos.write(97); //虽然写出的是一个int数,但是在写出的时候会将前面的24个0去掉,所以写出的一个byte } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) fos.close(); if (bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
1.5 拷贝音频文件画原理图
* A:案例演示
* 字节流一次读写一个字节复制音频
* 弊端:效率太低。举生活中买鸡蛋的例子。买鸡蛋,一次买一个,买一百次鸡蛋。让学生明白含义。
有没有一种办法一次性读完再一次性写完。
1.6 字节数组拷贝之available()方法
* A:案例演示
* int read(byte[] b):一次读取一个字节数组
* write(byte[] b):一次写出一个字节数组
* available()获取读的文件所有的字节个数
* 弊端:有可能会内存溢出
package com.se.file; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo05_available { public static void main(String[] args) { /** * 拷贝文件 * 1. 读取源文件文件当中的内容,用的就是输入流FileInputStream * 2. 新建一个文件,将读取到的内容写入到新建的文件当初,用的就是输出流,FileOutputStream * 3. 关闭输入流和输出流 */ FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("F:\Demo1\暴林 - 今夜的你又在和谁约会.mp3"); fos = new FileOutputStream("F:\Demo1\暴林 - 今夜的你又在和谁约会_02.mp3"); int length = fis.available(); //根据文件大小做一个字节数组 byte[] bytes = new byte[length]; // 声明一个和文件大小一样的byte数组 fis.read(bytes); //将文件上的所有字节读取到数组中 fos.write(bytes); //将数组中的所有字节一次写到了文件上 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) fis.close(); if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
1.7 定义小数组
* write(byte[] b)
* write(byte[] b, int off, int len)写出有效的字节个数
数据中的起始偏移量就是这里的数组的索引
package com.jhedu.day22; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo5_FileCopyArraySmall { public static void main(String[] args) throws IOException { /** * 拷贝文件 * 1. 读取源文件文件当中的内容,用的就是输入流FileInputStream * 2. 新建一个文件,将读取到的内容写入到新建的文件当初,用的就是输出流,FileOutputStream * 3. 声明一个小数组,大小为 1024, * 4. 使用文件输入read(byte[] b),读相应的文件 * 5. 写入的时候使用write(byte[] b, 0, b) * 6. 关闭输入流和输出流 */ FileInputStream fis = new FileInputStream("src\com\jhedu\day22\02.mp4"); FileOutputStream fos = new FileOutputStream("src\com\jhedu\day22\copy.mp4"); 7 byte[] bytes = new byte[1024 * 16]; // 注释第3行 int b; while ((b=fis.read(bytes)) !=-1) { fos.write(bytes, 0, b); } fis.close(); fos.close(); } }
1.8 BufferedInputStream 和 BufferedOutputStream 拷贝
* A:缓冲思想
* 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
* 这是加入了数组这样的缓冲区效果,java本身在设计的时候,
* 也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流
* B.BufferedInputStream
* BufferedInputStream内置了一个缓冲区(数组)
* 从BufferedInputStream中读取一个字节时
* BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个
* 程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
* 直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
* C.BufferedOutputStream
* BufferedOutputStream也内置了一个缓冲区(数组)
* 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
* 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
* D.拷贝的代码
FileInputStream fis = new FileInputStream("致青春.mp3"); //创建文件输入流对象,关联致青春.mp3 BufferedInputStream bis = new BufferedInputStream(fis); //创建缓冲区对fis装饰 FileOutputStream fos = new FileOutputStream("copy.mp3"); //创建输出流对象,关联copy.mp3 BufferedOutputStream bos = new BufferedOutputStream(fos); //创建缓冲区对fos装饰 int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close(); //只关装饰后的对象即可 bos.close();
* E.小数组的读写和带Buffered的读取哪个更快?
* 定义小数组如果是8192个字节大小和Buffered比较的话
* 定义小数组会略胜一筹,因为读和写操作的是同一个数组
* 而Buffered操作的是两个数组
Reade()一次表面是读一个,其实是读8192个字节。然后后再一个一个的给int b .b再一个一个往右边去装,装满8192个以后,再一次性的给写出去。这些都在中间方块内存中完成的,内存的运算效率要远远的高于硬盘。它是减少了到硬盘中读的次数和到硬盘上写的次数。
1.9 flush 和 close 方法的区别
* flush()方法
* 用来刷新缓冲区的,刷新后可以再次写出
* close()方法
* 用来关闭流释放资源的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出
Qq聊天中的时时刷新 需要用到flush 方法,
* close方法
* 具备刷新的功能,在关闭流之前,就会先刷新一次缓冲区,将缓冲区的字节全都刷新到文件上,再关闭,close方法刷完之后就不能写了
* flush方法?
* 具备刷新的功能,刷完之后还可以继续写
Close 后自带flush功能,不管也不刷新,会少内容。没有写进来完
2.0 字节流读写中文
* 字节流读取中文的问题
* 字节流在读中文的时候有可能会读到半个中文,造成乱码
* 字节流写出中文的问题
* 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
* 写出回车换行 write(" ".getBytes());
public class Demo06_Chinese { public static void main(String[] args) throws IOException { //demo01(); FileOutputStream fos = new FileOutputStream("zzz.txt"); fos.write("最近天冷了,注意穿秋裤啊。".getBytes());//write没有直接写字符串的参数。要转成getBytes fos.write(" ".getBytes()); fos.close(); } public static void demo01() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("yyy.txt"); byte[] arr= new byte[3];//你?媚?好一次读取一个字节。一个汉字是两个字节。 int len; while((len=fis.read(arr))!=-1) { System.out.print(new String(arr,0,len));//你?媚?好 } fis.close(); } }
2.2 IO 字符流 FileReader
* 1.字符流是什么
* 字符流是可以直接读写字符的IO流
* 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出.
* 2.FileReader
2.3 IO 字符流 FileWriter
FileWriter类的write()方法可以自动把字符转为字节写出
package com.jhedu.day23; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo3_FileReaderSmallArray { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("ccc.txt"); FileWriter fw = new FileWriter("aaa.txt"); /** * 1. 字节流读取的时候用的是byte数组 * 2. 字符流读取的时候用的就是char数组 */ char[] chars = new char[1024 * 8]; int b; while ((b=fr.read(chars)) !=-1){ fw.write(chars,0,b); } fw.close(); fr.close(); } }
2.4 什么情况下使用字符流
* 字符流也可以拷贝文本文件, 但不推荐使用. 因为读取时会把字节转为字符, 写出时还要把字符转回字节.
* 程序需要读取一段文本, 或者需要写出一段文本的时候可以使用字符流
* 读取的时候是按照字符的大小读取的,不会出现半个中文
* 写出的时候可以直接将字符串写出,不用转换为字节数组
2.5 字符流是否可以拷贝非纯文本的文件
拷贝非纯文本的文件(音频、视频、图片等)不可以使用字符流,即FileReader和FileWriter。
如果要拷贝非纯文本的文件,必须使用字节流,即FileInputStream和FileOutputStream。
* 不可以拷贝非纯文本的文件
* 因为在读的时候会将字节转换为字符,在转换过程中,可能找不到对应的字符,就会用?代替,写出的时候会将字符转换成字节写出去
* 如果是?,直接写出,这样写出之后的文件就乱了,看不了了
2.6 带缓冲区的字符流
* BufferedReader的read()方法:读取字符时会一次读取若干字符到缓冲区, 然后逐个返回给程序, 降低读取文件的次数, 提高效率
* BufferedWriter的write()方法:写出字符时会先写到缓冲区, 缓冲区写满时才会写到文件, 降低写文件的次数, 提高效率
*
BufferedReader br = new BufferedReader(new FileReader("aaa.txt")); //创建字符输入流对象,关联aaa.txt
BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt")); //创建字符输出流对象,关联bbb.txt
package com.jhedu.day23; import java.io.*; public class Demo4_FileReaderBuffer { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("ccc.txt"); FileWriter fw = new FileWriter("aaa.txt"); /** * 缓冲区的作用就相当于把字节流或者字符流进行了一次包装,提高了读文件和写文件的效率。 */ BufferedReader br = new BufferedReader(fr); //创建字符输入流对象,关联aaa.txt BufferedWriter bw = new BufferedWriter(fw); //创建字符输出流 int b; while((b = br.read()) != -1) { //read一次,会先将缓冲区读满,从缓冲去中一个一个的返给临时变量ch bw.write(b); //write一次,是将数据装到字符数组,装满后再一起写出去 } br.close(); //关流 bw.close(); } }
package com.se.file; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo07_FileReader { public static void main(String[] args) { FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader("F:\Demo1\w.txt"); fileWriter = new FileWriter("F:\Demo1\o.txt"); int b; while ((b = fileReader.read()) != -1) { fileWriter.write(b); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) fileReader.close(); if (fileWriter != null) fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2.7 readLine() 和 newLine() 方法
* BufferedReader的readLine()方法可以读取一行字符(不包含换行符号)
* BufferedWriter的newLine()可以输出一个跨平台的换行符号" "
2.8 LineNumberReader
* LineNumberReader是BufferedReader的子类, 具有相同的功能, 并且可以统计行号
* 调用getLineNumber()方法可以获取当前
* 调用setLineNumber()方法可以设置当前行号
package com.se.file; import java.io.*; public class Demo10_Line { public static void main(String[] args) { LineNumberReader lineNumberReader = null; try { FileReader fileReader = new FileReader("F:\Demo1\w.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); lineNumberReader = new LineNumberReader(bufferedReader); lineNumberReader.setLineNumber(0); // 设置行号,此处设置的是0,但是从1开始 String line; while ((line = lineNumberReader.readLine()) != null) { System.out.println(lineNumberReader.getLineNumber() + "--------" + line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (lineNumberReader != null) lineNumberReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2.9 使用指定的码表读写字符
* FileReader是使用默认码表读取文件, 如果需要使用指定码表读取, 那么可以使用InputStreamReader(字节流,编码表)
* FileWriter是使用默认码表写出文件, 如果需要使用指定码表写出, 那么可以使用OutputStreamWriter(字节流,编码表)
package com.se.file; import java.io.*; public class Demo11_编码 { public static void main(String[] args) { InputStreamReader inputStreamReader = null; OutputStreamWriter outputStreamWriter = null; try { inputStreamReader = new InputStreamReader(new FileInputStream("F:\Demo1\w.txt"), "utf-8"); outputStreamWriter = new OutputStreamWriter(new FileOutputStream("F:\Demo1\aaa.txt"), "gbk"); int n; while ((n = inputStreamReader.read()) != -1) { outputStreamWriter.write(n); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStreamReader != null) inputStreamReader.close(); if (outputStreamWriter != null) outputStreamWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3.0 转换流图解(扩展)
* 画图分析转换流
3.1 将文本反转
* 将一个文本文档上的文本反转,第一行和倒数第一行交换,第二行和倒数第二行交换
package com.se.file; import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class Demo09_Line { public static void main(String[] args) { // demo(); // 将一个文本文档上的文本反转,第一行和倒数第一行交换,第二行和倒数第二行交换 ArrayList<String> list = new ArrayList<>(); BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { FileReader fileReader = new FileReader("F:\Demo1\w.txt"); bufferedReader = new BufferedReader(fileReader); FileWriter fileWriter = new FileWriter("F:\Demo1\c.txt"); bufferedWriter = new BufferedWriter(fileWriter); String line; while ((line = bufferedReader.readLine()) != null) { list.add(line); } for (int i = list.size() - 1; i >= 0; i--) { System.out.println(list.get(i)); bufferedWriter.write(list.get(i)); bufferedWriter.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); if (bufferedWriter != null) bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void demo() { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = simpleDateFormat.format(date); System.out.println(time); } }