I/O流

流的分类

  1. 操作数据单位:字节流、字符流
  2. 数据的流向:输入流、输出流
  3. 流的角色:节点流、处理流

    流的体系结构

抽象基类 节点流(或文件流) 缓存流(处理流的一种)
InputStream FileInputStream BufferedInputStream
OutputStream FileOutputStream BufferedOutputStream
Reader FileReader BufferedReader
Writer FileWriter BufferedWriter

字符流输入且输出

  1. File file = new File("hello.txt");
  2. File filecopy = new File("hello1.txt");
  3. FileReader in = null;
  4. FileWriter out = null;
  5. try {
  6. char[] cur = new char[5];
  7. int len;
  8. //构造器true,不会覆盖
  9. out = new FileWriter(filecopy,true);
  10. in = new FileReader(file);
  11. while((len = in.read(cur))!=-1){
  12. out.write(cur,0,len);
  13. }
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }finally{
  17. if(out!=null){
  18. try {
  19. out.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. if(in!=null){
  25. try {
  26. in.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

字节流输入输出

  1. File file = new File("q.png");
  2. File filecopy = new File("q1.png");
  3. FileInputStream in = null;
  4. FileOutputStream out = null;
  5. try {
  6. //构造器true,不会覆盖
  7. byte[] cur = new byte[5];
  8. int len;
  9. out = new FileOutputStream(filecopy,true);
  10. in = new FileInputStream(file);
  11. while((len = in.read(cur))!=-1){
  12. out.write(cur,0,len);
  13. }
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }finally{
  17. if(out!=null){
  18. try {
  19. out.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. if(in!=null){
  25. try {
  26. in.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

处理流之一缓存流

提高流的读写速度,原因:内部提供了一个缓存区

  1. 代码
    1. //造文件
    2. File srcfile = new File("1.png");
    3. File destfile = new File("2.png");
    4. BufferedInputStream bis = null;
    5. BufferedOutputStream bos = null;
    6. try {
    7. //造流
    8. //造节点流
    9. FileInputStream fis = new FileInputStream(srcfile);
    10. FileOutputStream fos = new FileOutputStream(destfile);
    11. //造缓冲流
    12. bis = new BufferedInputStream(fis);
    13. bos = new BufferedOutputStream(fos);
    14. //复制的细节:读取、写入
    15. byte[] buffer = new byte[1024];
    16. int len;
    17. while((len = bis.read(buffer))!=-1){
    18. bos.write(buffer,0,len);
    19. }
    20. } catch (IOException e) {
    21. e.printStackTrace();
    22. }finally {
    23. if(bis!=null){
    24. try {
    25. bis.close();
    26. } catch (IOException e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. if(bos!=null){
    31. try {
    32. bos.close();
    33. } catch (IOException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37. }

    处理流的一种:转换流

读取UTF-8

  1. InputStreamReader isr = null;
  2. try {
  3. FileInputStream fis = new FileInputStream("UT8-8.TXT");
  4. isr = new InputStreamReader(fis, "UTF-8");
  5. char[] cbuf = new char[20];
  6. int len;
  7. while((len=isr.read(cbuf))!=-1){
  8. for(int i =0;i<cbuf.length;i++){
  9. System.out.println(cbuf[i]);
  10. }
  11. }
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }finally {
  15. if(isr!=null){
  16. try {
  17. isr.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }