黑马程序员_IO流(二)_File类、其他流

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

流操作的基本规律:

 


1、明确源和目的;

 


2、操作的数据是否为纯为本;

 


3、当体系明确后,在明确要使用哪个具体的对象。

 

 

File类:

 

 

用来将文件或者文件夹封装成对象。

 

 

方便对文件与文件夹的属性信息进行操作。

 

 

File对象可以作为参数传递给构造函数。

 

 

目录分隔符:

 

 

File f = new File("c:"+File.separator+"abc");可跨平台;

 

 

File常见方法:

 

 

    1、创建:

 

 

        boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。

 

 

        boolean mkdir();创建文件夹,只能创建一级目录;

 

 

        boolean mkdirs();可以创建多级文件夹;

 

 

        和输出流不一样,输出流对象一建立创建文件。而且文件已经存在,会覆盖。

 

 

    2、删除:

 

 

        boolean delete();删除失败返回false;

 

 

        void deleteOnExit();在程序退出时删除。

 

 

3、判断:

 

 

boolean exists();判断文件是否存在;

 

 

在判断文件对象是否是文件或者目的时,必须要先判断该文件对象封装的内容是否存在。

 

 

4、获取信息:

 

 

getName();

 

 

getPath();

 

 

getParent();

 

 

getAbsolutePath();

 

 

getAbsoluteFile();返回的是一个File类型,把绝对路径指向的文件直接封装为对象。

 

 

length();

 

 

lastModified();

 

 

renameTo(File f);重命名,还可以剪切文件。

 

 

调用list()方法的file对象必须是封装了一个目录,该目录还必须存在。

 

 

list(FilenameFilter filter)方法示例代码:

 

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.           
  3.         File dir = new File("d://java223//day2");  
  4.           
  5.         String[] arr = dir.list(new FilenameFilter(){  
  6.   
  7.             @Override  
  8.             public boolean accept(File dir, String name) {  
  9.                 if(name.endsWith(".bmp"))  
  10.                     return true;  
  11.                 else  
  12.                     return false;  
  13.             }  
  14.               
  15.         });  
  16.     }  

 

 

递归显示文件列表:

 


1、递归要注意限定条件;

 


2、要注意递归的次数,避免内存溢出。

 

 

 

Java代码   收藏代码
  1. public static void showDir(File dir) {  
  2.           
  3.         System.out.println(dir);  
  4.         File[] files = dir.listFiles();  
  5.         for(int x=0;x<files.length;x++) {  
  6.               
  7.             if(files[x].isDirectory()) {  
  8.                 showDir(files[x]);  
  9.             } else {  
  10.                 System.out.println(files[x]);  
  11.             }  
  12.         }  
  13.     }  

 

 

Properties类:

 

 

Properties是hashtable的子类,也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。

 

 

是集合中和IO技术相结合的集合容易。

 

 

该对象的特点:可以用于键值对形式的配置文件。

 

Java代码   收藏代码
  1. public static void method_1() throws IOException {  
  2.           
  3.         BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));  
  4.           
  5.         String line = null;  
  6.           
  7.         Properties prop = new Properties();  
  8.           
  9.         while((line=bufr.readLine()) != null) {  
  10.             String[] arr = line.split("=");  
  11.             prop.setProperty(arr[0], arr[1]);  
  12.         }  
  13.           
  14.         bufr.close();  
  15.     }  
  16.       
  17.     public static void setAndGet() {  
  18.           
  19.         Properties prop = new Properties();  
  20.           
  21.         prop.setProperty("zhangsan""30");  
  22.         prop.setProperty("lisi""39");  
  23.           
  24.         String value = prop.getProperty("lisi");  
  25.           
  26.         System.out.println(value);  
  27.           
  28.         Set<String> names = prop.stringPropertyNames();  
  29.           
  30.         for(String s : names) {  
  31.             System.out.println(s + ":" + prop.getProperty(s));  
  32.         }  
  33.     }  

 

 

打印流(PrintWriter,PrintStream):

 

 

打印流提供了打印方法,可以将各种数据类型的数据都原样打印。

 

 

构造方法:

 


1、file对象,File;

 


2、字符串路径,String;

 


3、字节输出流,OutputStream;

 


4、字符输出流,Writer;

 

 

自动刷新起作用的方法有,printf()、println()、format();

 

 

序列流(SequenceInputStream):

 

 

Java代码   收藏代码
  1. <strong>public static void main(String[] args) throws Exception {  
  2.           
  3.         Vector<FileInputStream> v = new Vector<FileInputStream>();  
  4.           
  5.         v.add(new FileInputStream("1.txt"));  
  6.         v.add(new FileInputStream("2.txt"));  
  7.         v.add(new FileInputStream("3.txt"));  
  8.           
  9.         Enumeration<FileInputStream> en = v.elements();//获取Enumeration  
  10.         SequenceInputStream sis = new SequenceInputStream(en);  
  11.           
  12.         FileOutputStream fos = new FileOutputStream("c\\4.txt");  
  13.         byte[] buf = new byte[1024];  
  14.           
  15.         int len = 0;  
  16.         while ((len=sis.read(buf))!=-1) {  
  17.             fos.write(buf, 0, len);  
  18.         }  
  19.           
  20.         fos.close();  
  21.         sis.close();  
  22.     }  
  23. </strong>  

 

 

切割文件:

 

Java代码   收藏代码
  1. public static void splitFile() throws IOException {  
  2.           
  3.         FileInputStream fis = new FileInputStream("c:\\1.bmp");  
  4.           
  5.         FileOutputStream fos = null;  
  6.           
  7.         byte[] buf = new byte[1024*1024];  
  8.           
  9.         int len = 0;  
  10.           
  11.         int count = 1;  
  12.           
  13.         while ((len=fis.read(buf))!=-1) {  
  14.             fos = new FileOutputStream("c:\\"+ (count++) +".part");  
  15.             fos.write(buf,0,len);  
  16.             fos.close();  
  17.         }  
  18.           
  19.         fis.close();  
  20.     }  

 

 

静态成员不能被序列化,因为静态在方法区,序列化只能对堆内的对象进行序列化。

 

 

Transient关键字也可以使成员不序列化。

 

 

管道流(PipedInputStream、PipedOutputStream):

 

 

输入输出可以直接进行连接,通过结合线程。

 

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.           
  3.         PipedInputStream in = new PipedInputStream();  
  4.         PipedOutputStream out = new PipedOutputStream();  
  5.           
  6.         in.connect(out);  
  7.         Read r = new Read(in);  
  8.         Write w = new Write(out);  
  9.           
  10.         new Thread(r).start();  
  11.         new Thread(w).start();  
  12.           
  13.     }  
  14.       
  15. }  
  16.   
  17. class Read implements Runnable {  
  18.   
  19.     private PipedInputStream in;  
  20.       
  21.     Read(PipedInputStream in) {  
  22.         this.in = in;  
  23.     }  
  24.     @Override  
  25.     public void run() {  
  26.   
  27.         int len;  
  28.         try {  
  29.             byte[] buf = new byte[1024];  
  30.               
  31.             len = in.read(buf);  
  32.               
  33.             String s = new String(buf,0,len);  
  34.               
  35.             System.out.println(s);  
  36.               
  37.             in.close();  
  38.               
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }   
  42.           
  43.           
  44.     }  
  45.       
  46. }  
  47.   
  48. class Write implements Runnable {  
  49.   
  50.     private PipedOutputStream out;  
  51.       
  52.     Write(PipedOutputStream out) {  
  53.         this.out = out;  
  54.     }  
  55.     @Override  
  56.     public void run() {  
  57.   
  58.         try {  
  59.             out.write("guandao lai le".getBytes());  
  60.         } catch (IOException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64.       
  65. }  

 

 

RandomAccessFile: 

 

 

RandomAccessFile不是IO体系中的子类,直接继承Object,但是它是IO包中的成员。因为它具备读和写功

 

 

能。内部封装了一个数组,而且通过指针对数组的元素进行操作。可以通过getFilePointer()获取指针位

 

 

置,同时可以通过seek改变指针的位置。该类只能操作文件。

 

 

如果模式为rw读写,如果要操作的文件不存在,会自动创建,如果存在则不会覆盖。如果模式为只读,不会

 

 

创建文件。会读取一个已存在的文件,如果该文件不存在,则会出现异常。

 

 

操作基本数据类型的流:

  

 

  DataStream:

 

 

操作字节数组:

 

 

    ByteArrayStream


---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值