Java I/O流

来源:互联网 发布:如何做好酒店网络销售 编辑:程序博客网 时间:2024/06/05 06:30

概念

流:流动,流向 从一端移动到另一端 源头和目的地
程序 与 文件|数组|网络连接|数据库,以程序为中心。

I/O流分类

  1. 流向:输入流和输出流
  2. 数据:
    • 字节流:二进制,可以一切文件,包括纯文本,doc,音频,视频等
    • 字符流:文本文件,只能处理纯文本,txt,html等
  3. 功能:
    • 节点流:包裹源头
    • 处理流:增强功能,提供性能

字节流和字符流与文件(重点)

  1. 字节流
    • 输入流:InputStream read(byte[] b),read(byte[] b, int off, int len) + close()
      • FileInputStream
    • 输出流:OutputStream write(byte[] b),write(byte[] b, int off, int len) + flush() + close()
      • FileOutputStream
  2. 字符流
    • 输入流:Reader read(char[] cbuf),read(char[] cbuf, int off, int len) + close()
      • FileReader()
    • 输出流:Writer write(char[] cbuf),write(char[] cbuf, int off, int len) + flush() + close()
      • FileWriter()

操作

  1. 建立联系
  2. 选择流
  3. 操作,数组大小 + read, write
  4. 释放资源
  • 读取文件
    1. 建立联系:File对象 源头
    2. 选择流:文件输入流 InputStream FileInputStream
    3. 操作:byte[] b = new byte[1024]; + read + 读取大小 输出
    4. 释放资源:关闭
  • 写出文件
    1. 建立联系:File对象 目的地
    2. 选择流:文件输出流 OutputStream FileOutputStream
    3. 操作:write() + flush
    4. 释放资源:关闭
  • 拷贝文件 程序为桥梁
    1. 建立联系 File对象 源头 目的地
    2. 选择流:
      • 文件输入流 InputStream FileInputStream
      • 文件输出流 OutputStream FileOutputStream
    3. 操作:拷贝
      • byte[] flush = new byte[1024];
      • int len = 0;
      • while(-1!=(len=输入流.read(flush))) {
        • 输出流.write(flush, 0, len);
      • }
      • 输出流.flush
    4. 释放资源:关闭 两个流

实例

  1. 读取文件
    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class Demo01 {public static void main(String[] args) {//1.建立联系File对象File src = new File("D:/test/1.txt");//2.选择流InputStream is = null; //提升作用域try {is = new FileInputStream(src); //3.操作 不断读取 缓冲数组byte[] b = new byte[1024];int len; //接收实际读取大小//循环读取while(-1!=(len=is.read(b))) {//输出 字节数组转字符串String info = new String(b, 0, len);System.out.println(info);}} catch (FileNotFoundException e) {System.out.println("文件不存在!");e.printStackTrace();} catch (IOException e) {e.printStackTrace();System.out.println("读取文件失败!");}finally {//4.释放资源if(null!=is) {try {is.close();} catch (IOException e) {e.printStackTrace();System.out.println("关闭文件输入流失败!");}}}}}

  2. 写出文件
    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Demo02 {public static void main(String[] args) {// 1.建立联系 File对象 目的地File dest = new File("D:/test/2.txt");// 2.选择流 文件输出流 OutputStream FileOutputStreamOutputStream os = null;// 以追加的形式写出文件try {os = new FileOutputStream(dest, true);String str = "THE CHO3EN 1";// 3.操作byte[] data = str.getBytes();// 字符串转字节数组os.write(data, 0, data.length);// 强制刷新出去os.flush();} catch (FileNotFoundException e) {e.printStackTrace();System.out.println("文件未找到!");} catch (IOException e) {e.printStackTrace();System.out.println("文件写入失败!");} finally {// 释放资源:关闭if (null != os) {try {os.close();} catch (IOException e) {e.printStackTrace();System.out.println("关闭输出流失败!");}}}}}

  3. 拷贝文件
    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class CopyFileDemo {public static void main(String[] args) throws IOException {// 1.建立联系 源(存在且为文件)+ 目的地(文件可以不存在)File src = new File("D:/test/1.txt");File dest = new File("D:/test/3.txt");// 2.选择流InputStream is = new FileInputStream(src);OutputStream os = new FileOutputStream(dest);// 3.文件拷贝 循环 + 读取 + 写出byte[] flush = new byte[1024];int len = 0;// 读取while (-1 != (len = is.read(flush))) {// 写出os.write(flush, 0, len);}// 强制刷出os.flush();// 4.关闭流os.close();is.close();}}

处理流

  • 处理流:增强功能,提供性能,节点流之上
    • 缓冲流
      • 字节缓冲流
        • BufferedInputStream
        • BufferedOutputStream
      • 字符缓冲流
        • BufferedReader  readLin()
        • BufferedWriter     newLine()
    • 转换流:字节流转为字符流 处理乱码(编码集,解码集)
      • 编码:字符 编码字符集->二进制
      • 解码:二进制 解码字符集->字符
      • 乱码:编码与解码的字符集不统一|字节缺少,长度丢失

实例

  1. 文件拷贝(字节处理流)
    import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class BufferedByteDemo {public static void main(String[] args) throws IOException {// 1.建立联系 源(存在且为文件)+ 目的地(文件可以不存在)File src = new File("D:/test/1.txt");File dest = new File("D:/test/3.txt");// 2.选择流InputStream is = new BufferedInputStream(new FileInputStream(src));OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));// 3.文件拷贝 循环 + 读取 + 写出byte[] flush = new byte[1024];int len = 0;// 读取while (-1 != (len = is.read(flush))) {// 写出os.write(flush, 0, len);}// 强制刷出os.flush();// 4.关闭流os.close();is.close();}}
  2. 文件拷贝(字符处理流)
    import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;public class BufferedCharDemo {public static void main(String[] args) throws IOException {File src = new File("D:/test/4.txt");File dest = new File("D:/test/5.txt");BufferedReader br = new BufferedReader(new FileReader(src));BufferedWriter bw = new BufferedWriter(new FileWriter(dest));String msg = null;while(null!=(msg=br.readLine())) {bw.write(msg);bw.newLine();}bw.flush();bw.close();br.close();}}
1 0
原创粉丝点击