码迷,mamicode.com
首页 > 其他好文 > 详细

黑马程序员——IO流总结

时间:2015-12-26 15:09:33      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

                ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- 
什么是IO流?
IO流是用来处理设备之间的数据传输
 
有哪些流?
流按操作数据的种类分为:字符流和字节流
流按流的方向分:输入流和输出流
 
字节流的基类:InputStream和OutputStream
字符流的基类:Writer和Reader
 
常用写字符流:FileWriter,BufferedWriter,OutputStreamWriter,PrinterWriter
常用读字符流  : FileReader,BufferedReader,OutputStreamReader
 
常用输入字节流:FileInputStream.BufferedInputStream
常用输出字节流:FileOutputStream,BufferedOutputStream,PrinterStream
 
装饰设计模式:把已有对象传入到一个新的类中,实现其功能的增强
 
File类常见操作:
1:创建
File file = new File("D:\\ee\\cc.txt");
file.createNewFile();
file.mkdir();
2:删除
file.delete();
file.deleteOnExit();
3:判断
file.exist();
file.isDirectory();
file.isFile();
file.isAbsloute()
file.isHidden()
4:获取
file.getPath();
file.getName();
file.getParent();
file.getAbsolutePath();
file.lastModified();
file.length();
file.listRoots();
file.list();
file.listFiles();
 1 练习
 2 package blogtest1;
 3 
 4 import java.io.FileWriter;
 5 
 6 /*
 7  * 对文件的续写
 8  */
 9 
10 public class IOTest1 {
11 
12  public static void main(String[] args)throws Exception {
13   FileWriter file = new FileWriter("C:\\Users\\Administrator\\Desktop\\ee.txt",true);
14   file.write("welcome to China");
15   file.close();
16 
17  }
18 
19 }
 1 练习
 2 package blogtest1;
 3 
 4 import java.io.File;
 5 
 6 public class IOTest2 {
 7 
 8  /**
 9   * 读取一个目录中所以.java文件,并打印到控制台
10   */
11  public static void main(String[] args) {
12  
13   File file = new File("F:\\yueming");
14   String[] str = file.list();
15   StringBuilder stringbuilder = new StringBuilder();
16   for(int i = 0 ; i < str.length; i++){
17    if(str[i].endsWith(".java")){
18     stringbuilder.append(str[i]+"\r\n");
19    }
20   }
21   System.out.println(stringbuilder);
22  }
23 
24 }
 1 练习
 2 package blogtest1;
 3 import java.io.*;
 4 
 5 public class IOTest3 {
 6 
 7  /**
 8   * 将C盘的文件复制到D盘
 9   */
10  public static void main(String[] args) throws Exception{
11   FileReader filereader = new FileReader("C:\\ee.txt");
12   FileWriter  filewriter = new FileWriter("D:\\aa.txt");
13   char[]  a = new char[1024];
14   int len = 0;
15   while((len = filereader.read(a))!= -1){
16    filewriter.write(a,0,len);
17   }
18   filereader.close();
19   filewriter.close();
20 
21  }
22 
23 }
 1 练习
 2 package blogtest1;
 3 import java.io.*;
 4 
 5 public class IOTest4 {
 6 
 7  /**
 8   * 将C盘的图片复制到D盘中
 9   */
10  public static void main(String[] args) throws Exception{
11   BufferedInputStream buff1 = new BufferedInputStream(new FileInputStream("C:\\dd.jpg"));
12   BufferedOutputStream buff2 = new BufferedOutputStream(new FileOutputStream("D:\\tt.jpg"));
13   byte[] a = new byte[1024];
14   int len = 0;
15   while((len = buff1.read(a))!= -1){
16    buff2.write(a,0,len);
17   }
18  
19 
20  }
21 
22 }
 1 练习
 2 package blogtest1;
 3 import java.util.*;
 4 import java.io.*;
 5 
 6 public class IOTest5 {
 7 
 8  /**
 9   * 演示Properties流功能
10   */
11  public static void main (String[] args) throws Exception{
12   Properties properties = new Properties();
13   properties.setProperty("zhangshan", "22");
14   properties.setProperty("lisi", "33");
15   properties.setProperty("wangwu","25");
16   properties.put("fuli", "45");
17   System.out.println(properties.getProperty("lisi"));
18  
19  
20   Set set = properties.keySet();
21   Iterator ite = set.iterator();
22   while(ite.hasNext()){
23    String key = (String)ite.next();
24    String value = (String)properties.get(key);
25    System.out.println(key + "....." + value);
26   }
27  
28  
29  
30  }
31 
32 }
 1 练习
 2 /*BufferedInputStream自定义实现*/
 3 import java.io.*;
 4 public class BufferedInputStreamTest
 5 {
 6  public static void main(String[] args) throws IOException
 7  {
 8   FileInputStream file1 = new FileInputStream("F:\\BaiduYunDownload\\第01天\\003_Java环境搭建(安装)_黑马程序员_Java基础视频.avi");
 9   FileOutputStream file2 = new FileOutputStream("D:\\yueyue.avi");
10   MyBufferedInputStream buff1 = new MyBufferedInputStream(file1);
11   BufferedOutputStream buff2 = new BufferedOutputStream(file2);
12   int len = 0;
13  
14  
15   while( (len = buff1.myRead()) != -1)
16   {
17    
18    buff2.write(len);
19   }
20 
21   buff1.myClose();
22   buff2.close();
23 
24 
25 
26  
27  }
28 }
29 
30 class MyBufferedInputStream
31 {
32  private InputStream file = null;
33  byte[] a = new byte[1024*4];
34  int pos = 0;
35  int count = 0;
36 
37  MyBufferedInputStream(InputStream file)
38  {
39   this.file = file;
40  }
41  public int myRead() throws IOException
42  {
43   if(count ==0)
44   {
45    count = file.read(a);
46    if(count<0)
47     return -1;
48    pos = 0;
49    byte b = a[pos];
50    count--;
51    pos++;
52    return b&255;
53   }
54   else if(count > 0)
55   {
56    byte b = a[pos];
57    count--;
58    pos++;
59    return b&255;
60   }
61   return -2;
62  }
63  public void myClose() throws IOException
64  {
65   file.close();
66  }
67 
68 
69 
70 
71 
72 }

 

黑马程序员——IO流总结

标签:

原文地址:http://www.cnblogs.com/yuemingxingxing/p/5078019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!