当前位置:   article > 正文

IO流的基础详解

IO流的基础详解

目录

【1】File类:

【2】引入:IO流:

【3】形象理解:IO流 当做一根 “管”:​编辑

【4】IO流的体系结构:​编辑

【5】案例:通过java程序完成文件的复制操作

功能分解1:文件 --》程序:FileReader

功能分解2:程序--》文件:FileWriter

功能分解3:利用FileReader,FileWriter文件复制

警告:不要用字符流去操作非文本文件

 利用try-catch-finally处理异常方式

【6】FileInputStream读取文件中内容

【1】读取文本文件:

【2】利用字节流读取非文本文件:(以图片为案例:)--》一个字节一个字节的读取:

【3】利用字节类型的缓冲数组:

【7】FileInputStream,FileOutputStream完成非文本文件的复制

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【8】缓冲字节流(处理流)-BufferedInputStream ,BufferedOutputStream

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【3】利用缓冲区:因为硬盘的访问次数是有限的,访问过多,就寿命到了。为了减少访问次数。程序中做了缓存区。

【9】比对非文本文件复制的三种方法的效率

【10】缓冲字符流(处理流)-BufferedReader,BufferedWriter完成文本文件的复制

【10】转换流-InputStreamReader,OutputStreamWriter

【1】转换流:作用:将字节流和字符流进行转换。【2】转换流  属于 字节流还是字符流?属于字符流

     InputStreamReader  :字节输入流 ---》字符的输入流    OutputStreamWriter  : 字符输出流 --》字节的输出流

【3】图解:

【4】将输入的字节流转换为输入的字符流,然后完成文件--》程序 :

【5】转换流-InputStreamReader,OutputStreamWriter实现文本文件的复制



【1】File类:

封装文件/目录的各种信息,对目录/文件进行操作,但是我们不可以获取到文件/目录中的内容。


【2】引入:IO流:

I/O : Input/Output的缩写,用于处理设备之间的数据的传输。


【3】形象理解:IO流 当做一根 “管”:

备注:判断一个流是输入的还是输出的,是以程序为基准,进入程序叫输入,出程序叫输出。

【4】IO流的体系结构:

【5】案例:通过java程序完成文件的复制操作

功能分解1:文件 --》程序:FileReader

  1. 一个字符一个字符的将文件中的内容读取到程序中了:
  2. package com.msb.io01;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. /**
  8. * @author : msb-zhaoss
  9. */
  10. public class Test01 {
  11. //这是一个main方法,是程序的入口:
  12. public static void main(String[] args) throws IOException {
  13. //文件--》程序:
  14. //1.有一个文件:----》创建一个File类的对象
  15. File f = new File("d:\\Test.txt");
  16. //2.利用FileReader这个流,这个“管”怼到源文件上去 ---》创建一个FileReader的流的对象
  17. FileReader fr = new FileReader(f);
  18. //3.进行操作“吸”的动作 ---》读取动作
  19. /*下面的代码我们验证了:如果到了文件的结尾处,那么读取的内容为-1
  20. int n1 = fr.read();
  21. int n2 = fr.read();
  22. int n3 = fr.read();
  23. int n4 = fr.read();
  24. int n5 = fr.read();
  25. int n6 = fr.read();
  26. System.out.println(n1);
  27. System.out.println(n2);
  28. System.out.println(n3);
  29. System.out.println(n4);
  30. System.out.println(n5);
  31. System.out.println(n6);*/
  32. //方式1
  33. /*int n = fr.read();
  34. while(n!=-1){
  35. System.out.println(n);
  36. n = fr.read();
  37. }*/
  38. //方式2
  39. int n;
  40. while((n = fr.read())!=-1){
  41. System.out.println((char)n); //ASCII码转字符
  42. }
  43. //4.“管”不用了,就要关闭 ---》关闭流
  44. //流,数据库,网络资源,靠jvm本身没有办法帮我们关闭,此时必须程序员手动关闭:
  45. fr.close();
  46. }
  47. }

 原文件内容:

多读的内容展示

  1. 想一次性读取五个字符,不够的话下次再读五个字符:
  2. package com.msb.io01;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. /**
  7. * @author : msb-zhaoss
  8. */
  9. public class Test02 {
  10. //这是一个main方法,是程序的入口:
  11. public static void main(String[] args) throws IOException {
  12. //文件--》程序:
  13. //1.创建一个File类的对象
  14. File f = new File("d:\\Test.txt");
  15. //2.创建一个FileReader的流的对象
  16. FileReader fr = new FileReader(f);
  17. //3.读取动作
  18. //引入一个“快递员的小车”,这个“小车”一次拉5个快递:
  19. char[] ch = new char[5];//缓冲数组
  20. int len = fr.read(ch);//一次读取五个:返回值是这个数组中 的有效长度
  21. while(len!=-1){
  22. //System.out.println(len);
  23. //错误方式:因为最后一次读取时,可能会多读
  24. /*for (int i = 0 ;i < ch.length;i++){
  25. System.out.println(ch[i]);
  26. }*/
  27. //正确方式:
  28. /*for (int i = 0 ;i < len;i++){
  29. System.out.println(ch[i]);
  30. }*/
  31. //正确方式2:将数组转为String
  32. String str = new String(ch,0,len);
  33. System.out.print(str);
  34. len = fr.read(ch);
  35. }
  36. //4.关闭流
  37. fr.close();
  38. }
  39. }

功能分解2:程序--》文件:FileWriter

  1. package com.msb.io01;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. /**
  6. * @author : msb-zhaoss
  7. */
  8. public class Test03 {
  9. //这是一个main方法,是程序的入口:
  10. public static void main(String[] args) throws IOException {
  11. //1.有个目标文件:
  12. File f = new File("d:\\demo.txt");
  13. //2.FileWriter管怼到文件上去:
  14. FileWriter fw = new FileWriter(f);
  15. //3.开始动作:输出动作:
  16. //一个字符一个字符的往外输出:
  17. String str = "hello你好";
  18. for (int i = 0 ;i < str.length();i++){
  19. fw.write(str.charAt(i));
  20. }
  21. //4.关闭流:
  22. fw.close();
  23. }
  24. }

备注:
如果目标文件不存在的话,那么会自动创建此文件。
如果目标文件存在的话:
new FileWriter(f)   相当于对原文件进行覆盖操作。
new FileWriter(f,false)  相当于对源文件进行覆盖操作。不是追加。
 new FileWriter(f,true)   对原来的文件进行追加,而不是覆盖。

利用缓冲数组:向外输出(利用缓冲数组:)

  1. package com.msb.io01;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. /**
  6. * @author : msb-zhaoss
  7. */
  8. public class Test03 {
  9. //这是一个main方法,是程序的入口:
  10. public static void main(String[] args) throws IOException {
  11. //1.有个目标文件:
  12. File f = new File("d:\\demo.txt");
  13. //2.FileWriter管怼到文件上去:
  14. FileWriter fw = new FileWriter(f,true);
  15. //3.开始动作:输出动作:
  16. //一个字符一个字符的往外输出:
  17. String str = "你好中国";
  18. char[] chars = str.toCharArray();
  19. fw.write(chars);
  20. //4.关闭流:
  21. fw.close();
  22. }
  23. }

功能分解3:利用FileReader,FileWriter文件复制

  1. package com.msb.io01;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test04 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //1.有一个源文件
  10. File f1 = new File("d:\\Test.txt");
  11. //2.有一个目标文件:
  12. File f2 = new File("d:\\Demo.txt");
  13. //3.搞一个输入的管 怼到源文件上:
  14. FileReader fr = new FileReader(f1);
  15. //4.搞一个输出的管,怼到目标文件上:
  16. FileWriter fw = new FileWriter(f2);
  17. //5.开始动作:
  18. //方式1:一个字符一个字符的复制:
  19. /*int n = fr.read();
  20. while(n!=-1){
  21. fw.write(n);
  22. n = fr.read();
  23. }*/
  24. //方式2:利用缓冲字符数组:
  25. /*char[] ch = new char[5];
  26. int len = fr.read(ch);
  27. while(len!=-1){
  28. fw.write(ch,0,len);//将缓冲数组中有效长度写出
  29. len = fr.read(ch);
  30. }*/
  31. //方式3:利用缓冲字符数组,将数组转为String写出。
  32. char[] ch = new char[5];
  33. int len = fr.read(ch);
  34. while(len!=-1){
  35. String s = new String(ch,0,len);
  36. fw.write(s);
  37. len = fr.read(ch);
  38. }
  39. //6.关闭流:(关闭流的时候,倒着关闭,后用先关)
  40. fw.close();
  41. fr.close();
  42. }
  43. }

警告:不要用字符流去操作非文本文件

文本文件:.txt   .java  .c  .cpp  ---》建议使用字符流操作
非文本文件:.jpg,  .mp3  ,   .mp4 , .doc  , .ppt  ---》建议使用字节流操作

 利用try-catch-finally处理异常方式

  1. package com.msb.io01;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test04 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) {
  9. //1.有一个源文件
  10. File f1 = new File("d:\\Test.txt");
  11. //2.有一个目标文件:
  12. File f2 = new File("d:\\Demo.txt");
  13. //3.搞一个输入的管 怼到源文件上:
  14. FileReader fr = null;
  15. FileWriter fw = null;
  16. try {
  17. fr = new FileReader(f1);
  18. //4.搞一个输出的管,怼到目标文件上:
  19. fw = new FileWriter(f2);
  20. //5.开始动作:
  21. char[] ch = new char[5];
  22. int len = fr.read(ch);
  23. while(len!=-1){
  24. String s = new String(ch,0,len);
  25. fw.write(s);
  26. len = fr.read(ch);
  27. }
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } finally {
  33. //6.关闭流:(关闭流的时候,倒着关闭,后用先关)
  34. try {
  35. if(fw!=null){//防止空指针异常
  36. fw.close();
  37. }
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. try {
  42. if(fr!=null){
  43. fr.close();
  44. }
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }

【6】FileInputStream读取文件中内容

【1】读取文本文件:

  1. package com.msb.io02;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. /**
  7. * @author : msb-zhaoss
  8. */
  9. public class Test01 {
  10. //这是一个main方法,是程序的入口:
  11. public static void main(String[] args) throws IOException {
  12. //功能:利用字节流将文件中内容读到程序中来:
  13. //1.有一个源文件:
  14. File f = new File("D:\\Test.txt");
  15. //2.将一个字节流这个管 怼 到 源文件上:
  16. FileInputStream fis = new FileInputStream(f);
  17. //3.开始读取动作
  18. /*
  19. 细节1
  20. 文件是utf-8进行存储的,所以英文字符 底层实际占用1个字节
  21. 但是中文字符,底层实际占用3个字节。如果用字节流读文件会将一个中文汉字读取三次,拆成三
  22. 个字节,不好知道,哪几个字节是那个字符
  23. 细节2
  24. 如果文件是文本文件,那么就不要使用字节流读取了,建议使用字符流。
  25. 细节3
  26. read()读取一个字节,但是你有没有发现返回值是 int类型,而不是byte类型?
  27. read方法底层做了处理,让返回的数据都是“正数”
  28. 就是为了避免如果字节返回的是-1的话,那到底是读入的字节,还是到文件结尾呢。
  29. */
  30. int n = fis.read();
  31. while(n!=-1){
  32. System.out.println(n);
  33. n = fis.read();
  34. }
  35. //4.关闭流:
  36. fis.close();
  37. }
  38. }

【2】利用字节流读取非文本文件:(以图片为案例:)--》一个字节一个字节的读取:

  1. package com.msb.io02;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. /**
  6. * @author : msb-zhaoss
  7. */
  8. public class Test02 {
  9. //这是一个main方法,是程序的入口:
  10. public static void main(String[] args) throws IOException {
  11. //功能:利用字节流将文件中内容读到程序中来:
  12. //1.有一个源文件:
  13. File f = new File("D:\\LOL.jpg");
  14. //2.将一个字节流这个管 怼 到 源文件上:
  15. FileInputStream fis = new FileInputStream(f);
  16. //3.开始读取动作
  17. int count = 0;//定义一个计数器,用来计读入的字节的个数
  18. int n = fis.read();
  19. while(n!=-1){
  20. count++;
  21. System.out.println(n);
  22. n = fis.read();
  23. }
  24. System.out.println("count="+count);
  25. //4.关闭流:
  26. fis.close();
  27. }
  28. }

【3】利用字节类型的缓冲数组:

  1. package com.msb.io02;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. /**
  6. * @author : msb-zhaoss
  7. */
  8. public class Test03 {
  9. //这是一个main方法,是程序的入口:
  10. public static void main(String[] args) throws IOException {
  11. //功能:利用字节流将文件中内容读到程序中来:
  12. //1.有一个源文件:
  13. File f = new File("D:\\LOL.jpg");
  14. //2.将一个字节流这个管 怼 到 源文件上:
  15. FileInputStream fis = new FileInputStream(f);
  16. //3.开始读取动作
  17. //利用缓冲数组:(快递员的小车)
  18. byte[] b = new byte[1024*6];
  19. int len = fis.read(b);//len指的就是读取的数组中的有效长度
  20. while(len!=-1){
  21. //System.out.println(len);
  22. for(int i = 0;i<len;i++){
  23. System.out.println(b[i]);
  24. }
  25. len = fis.read(b);
  26. }
  27. //4.关闭流:
  28. fis.close();
  29. }
  30. }

【7】FileInputStream,FileOutputStream完成非文本文件的复制

【1】读入一个字节,写出一个字节:

  1. package com.msb.io02;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test04 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //功能:完成图片的复制:
  10. //1.有一个源图片
  11. File f1 = new File("d:\\LOL.jpg");
  12. //2.有一个目标图片:
  13. File f2 = new File("d:\\LOL2.jpg");
  14. //3.有一个输入的管道 怼 到 源文件:
  15. FileInputStream fis = new FileInputStream(f1);
  16. //4.有一个输出的管道 怼到 目标文件上:
  17. FileOutputStream fos = new FileOutputStream(f2);
  18. //5.开始复制:(边读边写)
  19. int n = fis.read();
  20. while(n!=-1){
  21. fos.write(n);
  22. n = fis.read();
  23. }
  24. //6.关闭流:(倒着关闭流,先用后关)
  25. fos.close();
  26. fis.close();
  27. }
  28. }

【2】利用缓冲字节数组:
 

  1. package com.msb.io02;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. /**
  7. * @author : msb-zhaoss
  8. */
  9. public class Test05 {
  10. //这是一个main方法,是程序的入口:
  11. public static void main(String[] args) throws IOException {
  12. //功能:完成图片的复制:
  13. //1.有一个源图片
  14. File f1 = new File("d:\\LOL.jpg");
  15. //2.有一个目标图片:
  16. File f2 = new File("d:\\LOL2.jpg");
  17. //3.有一个输入的管道 怼 到 源文件:
  18. FileInputStream fis = new FileInputStream(f1);
  19. //4.有一个输出的管道 怼到 目标文件上:
  20. FileOutputStream fos = new FileOutputStream(f2);
  21. //5.开始复制:(边读边写)
  22. //利用缓冲数组:
  23. byte[] b = new byte[1024*8];
  24. int len = fis.read(b);
  25. while(len!=-1){
  26. fos.write(b,0,len);//将数组中,有效长度写进,防止多写自己
  27. len = fis.read(b);
  28. }
  29. //6.关闭流:(倒着关闭流,先用后关)
  30. fos.close();
  31. fis.close();
  32. }
  33. }

【8】缓冲字节流(处理流)-BufferedInputStream ,BufferedOutputStream

【1】读入一个字节,写出一个字节:

【2】利用缓冲字节数组:

【3】利用缓冲区:因为硬盘的访问次数是有限的,访问过多,就寿命到了。为了减少访问次数。程序中做了缓存区。

成上面的效果,单纯的靠FileInputStream,FileOutputStream是不可以完成的,这个时候就需要功能的加强,
这个加强就需要引入新的管道(在FileInputStream,FileOutputStream外面再套一层流):BufferedInputStream ,BufferedOutputStream. ----->处理流

代码:

  1. package com.msb.io02;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test06 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //1.有一个源图片
  10. File f1 = new File("d:\\LOL.jpg");
  11. //2.有一个目标图片:
  12. File f2 = new File("d:\\LOL2.jpg");
  13. //3.有一个输入的管道 怼 到 源文件:
  14. FileInputStream fis = new FileInputStream(f1);
  15. //4.有一个输出的管道 怼到 目标文件上:
  16. FileOutputStream fos = new FileOutputStream(f2);
  17. //5.功能加强,在FileInputStream外面套一个管:BufferedInputStream:
  18. BufferedInputStream bis = new BufferedInputStream(fis);
  19. //6.功能加强,在FileOutputStream外面套一个管:BufferedOutputStream:
  20. BufferedOutputStream bos = new BufferedOutputStream(fos);
  21. //7.开始动作 :
  22. byte[] b = new byte[1024*6];
  23. int len = bis.read(b);
  24. while(len!=-1){
  25. bos.write(b,0,len);
  26. /* bos.flush(); 底层已经帮我们做了刷新缓冲区的操作,缓存区满了后会自动推到目标文件
  27. 中去,不用我们手动完成:底层调用flushBuffer()*/
  28. len = bis.read(b);
  29. }
  30. //8.关闭流:
  31. //倒着关:
  32. //如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
  33. bos.close();
  34. bis.close();
  35. /*fos.close();
  36. fis.close();*/
  37. }
  38. }

【9】比对非文本文件复制的三种方法的效率

  1. package com.msb.io02;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test06 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //1.有一个源图片
  10. File f1 = new File("d:\\LOL.jpg");
  11. //2.有一个目标图片:
  12. File f2 = new File("d:\\LOL2.jpg");
  13. //3.有一个输入的管道 怼 到 源文件:
  14. FileInputStream fis = new FileInputStream(f1);
  15. //4.有一个输出的管道 怼到 目标文件上:
  16. FileOutputStream fos = new FileOutputStream(f2);
  17. //5.功能加强,在FileInputStream外面套一个管:BufferedInputStream:
  18. BufferedInputStream bis = new BufferedInputStream(fis);
  19. //6.功能加强,在FileOutputStream外面套一个管:BufferedOutputStream:
  20. BufferedOutputStream bos = new BufferedOutputStream(fos);
  21. //7.开始动作 :
  22. long startTime = System.currentTimeMillis();
  23. byte[] b = new byte[1024];
  24. int len = bis.read(b);
  25. while(len!=-1){
  26. bos.write(b,0,len);
  27. /* bos.flush(); 底层已经帮我们做了刷新缓冲区的操作,不用我们手动完成:底层调用flushBuffer()*/
  28. len = bis.read(b);
  29. }
  30. long endTime = System.currentTimeMillis();
  31. System.out.println("复制完成的时间为:"+(endTime-startTime));
  32. //8.关闭流:
  33. //倒着关:
  34. //如果处理流包裹着节点流的话,那么其实只要关闭高级流(处理流),那么里面的字节流也会随之被关闭。
  35. bos.close();
  36. bis.close();
  37. /*fos.close();
  38. fis.close();*/
  39. }
  40. }

【10】缓冲字符流(处理流)-BufferedReader,BufferedWriter完成文本文件的复制

  1. package com.msb.io02;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test07 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //1.有一个源文件:
  10. File f1 = new File("d:\\Test.txt");
  11. //2.有一个目标文件:
  12. File f2 = new File("d:\\Demo.txt");
  13. //3.需要一个管 怼到 源文件:
  14. FileReader fr = new FileReader(f1);
  15. //4.需要一根管怼到目标文件:
  16. FileWriter fw = new FileWriter(f2);
  17. //5.套一根管在输入字符流外面:
  18. BufferedReader br = new BufferedReader(fr);
  19. //6.套一根管在输出字符流外面:
  20. BufferedWriter bw = new BufferedWriter(fw);
  21. //7.开始动作:
  22. //方式1:读取一个字符,输出一个字符:
  23. /*int n = br.read();
  24. while(n!=-1){
  25. bw.write(n);
  26. n = br.read();
  27. }*/
  28. //方式2:利用缓冲数组:
  29. /*char[] ch = new char[30];
  30. int len = br.read(ch);
  31. while(len!=-1){
  32. bw.write(ch,0,len);
  33. len = br.read(ch);
  34. }*/
  35. //方式3:读取String
  36. String str = br.readLine();//每次读取文本文件中一行,返回字符串
  37. while(str!=null){
  38. bw.write(str);
  39. //在文本文件中应该再写出一个换行:
  40. bw.newLine();//新起一行
  41. str = br.readLine();
  42. }
  43. //8.关闭流
  44. bw.close();
  45. br.close();
  46. }
  47. }

【10】转换流-InputStreamReader,OutputStreamWriter

【1】转换流:作用:将字节流和字符流进行转换。
【2】转换流  属于 字节流还是字符流?属于字符流

     InputStreamReader  :字节输入流 ---》字符的输入流
    OutputStreamWriter  : 字符输出流 --》字节的输出流

【3】图解:

【4】将输入的字节流转换为输入的字符流,然后完成文件--》程序 :

  1. package com.msb.io03;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test01 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //文件---》程序:
  10. //1.有一个源文件:
  11. File f = new File("d:\\Test.txt");
  12. //2.需要一个输入的字节流接触文件:
  13. FileInputStream fis = new FileInputStream(f);
  14. //3.加入一个转换流,将字节流转换为字符流:(转换流属于一个处理流)
  15. //将字节转换为字符的时候,需要指定一个编码,这个编码跟原文件本身的编码格式统一
  16. //如果编码格式不统一的话,那么在控制台上展示的效果就会出现乱码
  17. //InputStreamReader isr = new InputStreamReader(fis,"utf-8");
  18. //获取当前idea控制台,或者叫 程序本身的编码
  19. InputStreamReader isr = new InputStreamReader(fis);
  20. //4.开始动作,将文件中内容显示在控制台:
  21. char[] ch = new char[20];
  22. int len = isr.read(ch);
  23. while(len!=-1){
  24. //将缓冲数组转为字符串在控制台上打印出来
  25. System.out.print(new String(ch,0,len));
  26. len = isr.read(ch);
  27. }
  28. //5.关闭流:
  29. isr.close();
  30. }
  31. }

【5】转换流-InputStreamReader,OutputStreamWriter实现文本文件的复制

  1. package com.msb.io03;
  2. import java.io.*;
  3. /**
  4. * @author : msb-zhaoss
  5. */
  6. public class Test02 {
  7. //这是一个main方法,是程序的入口:
  8. public static void main(String[] args) throws IOException {
  9. //1.有一个源文件
  10. File f1 = new File("d:\\Test.txt");
  11. //2.有一个目标文件:
  12. File f2 = new File("d:\\Demo.txt");
  13. //3.输入方向:
  14. FileInputStream fis = new FileInputStream(f1);
  15. InputStreamReader isr = new InputStreamReader(fis,"utf-8");
  16. //4.输出方向:
  17. FileOutputStream fos = new FileOutputStream(f2);
  18. OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
  19. //5.开始动作:
  20. char[] ch = new char[20];
  21. int len = isr.read(ch);
  22. while(len!=-1){
  23. osw.write(ch,0,len);
  24. len = isr.read(ch);
  25. }
  26. //6.关闭流:
  27. osw.close();
  28. isr.close();
  29. }
  30. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/403524
推荐阅读
相关标签
  

闽ICP备14008679号