A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lvwangxiao 中级黑马   /  2016-3-21 21:54  /  750 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;

  6. /*
  7. * 字符流
  8. *         Writer:
  9. *                 FileWriter
  10. *  Reader:
  11. *          FileReader
  12. *  
  13. *  字节流
  14. *   OutputStream:
  15. *           FileOutputStream
  16. *   
  17. *   InputStream:
  18. *           FileInputStream
  19. *   
  20. *   字符流/字节流完成文件复制
  21. *   从a.txt读取内容
  22. *   将读取到的内容写入到b.txt中
  23. *   
  24. */
  25. public class Demo2WriterReader {
  26.         public static void main(String[] args) throws IOException{
  27.                 method4();
  28.         }
  29.        
  30.        
  31.         //一次复制一个字符
  32.         public static void method() throws IOException{
  33.                
  34.                 //创建流对象
  35.                 FileReader fr = new FileReader("a.txt");
  36.                 FileWriter fw = new FileWriter("b.txt");
  37.                
  38.                 //先读
  39.                 int c;
  40.                 while((c=fr.read())!=-1){
  41.                         //后写
  42.                         fw.write(c);
  43.                 }
  44.                
  45.                
  46.                 //关闭流
  47.                 fw.close();
  48.                 fr.close();
  49.         }
  50.        
  51.        
  52.         //一次复制一个字符数组
  53.         public static void method2() throws IOException{
  54.                
  55.                 //创建流对象
  56.                 FileReader fr = new FileReader("D:/Developement/code/myday17/src/com/itheima1/MyNotepad.java");
  57.                 FileWriter fw = new FileWriter("b.java");
  58.                
  59.                 //先读
  60.                 char[] chars = new char[1024];
  61.                 int len;
  62.                 while((len=fr.read(chars))!=-1){
  63.                         //后写
  64.                         fw.write(chars,0,len);
  65.                 }
  66.                
  67.                 //关闭流
  68.                
  69.                 fw.close();
  70.                 fr.close();
  71.         }
  72.        
  73.        
  74.         //字节流一次一个字节
  75.         public static void method3() throws IOException{
  76.                 //创建流对象
  77.                 FileInputStream fis = new FileInputStream("panda.ico");
  78.                 FileOutputStream fos = new FileOutputStream("b.ico");
  79.                
  80.                 //先读
  81.                 int c;
  82.                 while((c=fis.read())!=-1){
  83.                         //后写
  84.                         fos.write(c);
  85.                 }
  86.                
  87.                 //关闭流
  88.                 fos.close();
  89.                 fis.close();
  90.         }
  91.        
  92.        
  93.         //字节流一次一个数组
  94.         public static void method4() throws IOException{
  95.                
  96.                 //创建流对象
  97.                 FileInputStream  fis = new FileInputStream("panda.ico");
  98.                 FileOutputStream fos = new FileOutputStream("c.ico");
  99.                
  100.                 //先读取
  101.                 byte[] chars = new byte[1024];
  102.                 int len;
  103.                 while((len=fis.read(chars))!=-1){
  104.                         fos.write(chars, 0, len);
  105.                 }
  106.                
  107.                 //关闭流
  108.                 fis.close();
  109.                 fos.close();
  110.         }
  111.        
复制代码

2 个回复

倒序浏览
刚好我要练习今天的代码。。。
回复 使用道具 举报
这是多少天的内容了,表示看不懂?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马