------- android培训、java培训、期待与您交流! ----------
字符流的缓冲区
缓冲区的出现提高了对数据的读写效率。
对应类
• BufferedWriter
• BufferedReader
缓冲区要结合流才可以使用。
在流的基础上对流的功能进行了增强。
字符写入流缓冲区:
该缓冲区中提供了一个跨平台的换行符。 newLine();
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; class BufferedWriterDemo { public static void main(String[] args) throws IOException { //创建一个字符写入流对象。 FileWriter fw = new FileWriter("BufferedWriterDemo.txt"); //为了提高字符写入流效率。加入了缓冲技术。 //只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。 BufferedWriter bw = new BufferedWriter(fw); for (int i=0; i<5; i++) { bw.write("abcde"); bw.newLine(); bw.flush();//记住,只要用到缓冲区,就要记得刷新。 } //其实关闭缓冲区,就是在关闭缓冲区中的流对象。 bw.close(); } }
字符读取流缓冲区:
该缓冲区提供了一个一次读一行的方法 readLine,方便于对文本数据的获取。
当返回null时,表示读到文件末尾。
readLine方法返回的时候只返回回车符之前的数据内容。并不返回回车符。
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class BufferedReaderDemo { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("BufferedWriterDemo.txt"); BufferedReader br = new BufferedReader(fr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * * 通过缓冲区复制文件练习 * */ class BufferedCopyTest { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader("E:\JAVA\Demo\BufferedWriterDemo.txt")); bw = new BufferedWriter(new FileWriter("E:\JAVA\Demo\BufferedCopyTest.txt")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } } catch (IOException e) { throw new RuntimeException("读写出错"); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { throw new RuntimeException("读取关闭出错"); } try { if (bw != null) { bw.close(); } } catch (IOException e) { throw new RuntimeException("写入关闭出错"); } } } }
字节流:
基本操作与字符流类相同。
但它不仅可以操作字符,还可以操作其他媒体文件。
字节流的缓冲区:同样是提高了字节流的读写效率。
转换流:
InputStreamReader,OutputStreamWriter
转换流的由来:
• 字符流与字节流之间的桥梁
• 方便了字符流与字节流之间的操作
转换流的应用:
• 字节流中的数据都是字符时,转成字符流操作更高效。
字符和字节之间的桥梁,通常,涉及到字符编码转换时, 需要用到转换流。
<span style="font-size:14px;">import java.io.FileOutputStream; import java.io.IOException; class OutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fs = new FileOutputStream("OutStreamDemo.txt"); fs.write("abcde".getBytes()); fs.close(); } } </span>
<span style="font-size:14px;">import java.io.FileInputStream; class InputStreamDemo { public static void main(String[] args) throws Exception { FileInputStream fs = new FileInputStream("OutStreamDemo.txt"); byte[] by = new byte[1024]; int num = 0; while ((num = fs.read(by)) != -1) { System.out.println(new String(by,0,num)); } fs.close(); } } </span>
为什么用字节流处理非文本,字符流处理文本?
字节流是用八位的字节直接代表图片进行转换。
字符流在复制图片的时候,有一些代码能在编码表中找到编码,就转换成编码,但是还有好多找不到的代码,
JVM就会用一些类似的编码代替,文件就会改变了,所以打不开
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 复制一个图片 * 思路: * 1,用字节读取流对象和图片关联。 * 2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。 * 3,通过循环读写,完成数据的存储。 * 4,关闭资源。 * */ class PictureCopyTest { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("photo.jpg"); fos = new FileOutputStream("photocopy.jpg"); byte[] by = new byte[1024]; int num = 0; while ((num = fis.read(by)) != -1) { fos.write(by,0,num); } } catch (IOException e) { throw new RuntimeException("读写失败"); } finally { try { fis.close(); } catch (IOException e) { throw new RuntimeException("读取关闭失败"); } try { fos.close(); } catch (IOException e) { throw new RuntimeException("写入关闭失败"); } } } }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 通过缓冲区演示复制视频 * */ class MoveCopyTest { public static void main(String[] args) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream("dog.mp4")); bos = new BufferedOutputStream(new FileOutputStream("pipi.mp4")); byte[] by = new byte[1024]; int num = 0; while ((num = bis.read(by)) != -1) { bos.write(by,0,num); } } catch (IOException e) { throw new RuntimeException("复制失败"); } finally { try { bis.close(); } catch (IOException e) { throw new RuntimeException("读取关闭失败"); } try { bos.close(); } catch (IOException e) { throw new RuntimeException("写入关闭失败"); } } } }