基础知识之----------IO流(3)

1、递归

需求:删除非空的文件夹目录

  

    public static void main(String[] args) {
        File dir=new File("d:\\file2");
//        testDo(dir);
        dir.delete();
    }

    public static void testDo(File dir) {

        File[] files=dir.listFiles();

        for(File file:files){
            if(file.isDirectory()){
                testDo(file);
            }else {
                file.delete();
            }
        }
        dir.delete();
    }

2、IO流(Properties集合的-基本功能

  Map-----Hashtable---Properties

  Properties 类表示 了一个持久的属性集。Properties可保存在流中或从流中加载。属性列表中每个键及其对应值就是一个字符串。

  Properties 特点:

          (1)该集合中的键和值都是字符串类型。

          (2)集合中的数据可以保存到流中,或者从流获取。

  通常该集合用于操作以键值对形式存在的配置文件。

    基本功能演示,存入然后打印出来。

  

 public static void propertiesDemo(){

        //创建一个Properties集合
        Properties prop=new Properties();

        //存储元素
        prop.setProperty("zhangsan","22");
        prop.setProperty("lisi","22");
        prop.setProperty("lisan","22");

        //取出所有元素

        Set<String> names=prop.stringPropertyNames();
        for(String name:names){
            String value=prop.getProperty(name);
            System.out.println(name+":"+value);
        }
    }

 

Properties集合和流相结合的功能-----持久化

  store(OutputStream out,String comments) 以使用使用load(Reader)方法的格式,将此Properties表中的属性列表(键和元素对)写入输出字符

  store使用了ISO 8859-1,因此尽量不要使用中文注释

  

    public static void methodDemo3() throws IOException {
        Properties prop = new Properties();
        prop.setProperty("zhangsan", "22");
        prop.setProperty("lisi", "22");
        prop.setProperty("lisan", "22");
        //想要将这些集合中的字符串键值信息持久化存储到文件中
        //需要关联输出流
        FileOutputStream fos=new FileOutputStream("info.txt");

        //将集合中数据存储到文件中,使用store方法
        prop.store(fos,"name+age");

        fos.close();
    }

  

  假如集合中的数据来自于一个文件。

  

 public static void methodDemo4() throws IOException {
        Properties prop=new Properties();
        //集合中的数据来自于一个文件。
        //注意:必须要保证该文件中的数据是键值对。
        //需要使用到读取流
        FileInputStream fis=new FileInputStream("info.txt");
        //使用load方法,将从流中获取集合数据
        prop.load(fis);
        prop.list(System.out);
    }


    //模拟load方法
    public static void myLoad() throws IOException {
        Properties prop = new Properties();
        BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));
        String line=null;
        while ((line=bufr.readLine())!=null){
            if(line.startsWith("#")){
                continue;
            }
            String[] arr=line.split("=");
//            System.out.println(arr[0]+":"+arr[1]);
            prop.setProperty(arr[0],arr[1]);
        }
        prop.list(System.out);
        bufr.close();
    }

 对已有配置文件进行修改

  

//对已有配置文件进行修改
    /*
    * 读取这个文件
    * 并将这个文件中的键值数据存储到集合中。
    * 再通过集合对数据进行修改
    * 再通过流将修改后的数据进存储到文件中
    *
    * */
    public static void methodDemo5() throws IOException {
        //读取这个文件
        File file=new File("info.txt");
        if (!file.exists()){
            file.createNewFile();
        }

        FileReader fr=new FileReader("info.txt");
        //创建集合存储配置信息
        Properties prop=new Properties();

        //将流中的信息存储到集合中
        prop.load(fr);

        prop.setProperty("lisan","23");


        FileWriter fw=new FileWriter(file);

        prop.store(fw,"");


        prop.list(System.out);

        fr.close();

    }

 

  需求:定义功能,获取一个应用程序运行的次数,如果超过5此,给出使用次数已到请注册的提示。并不要运行程序。

  

package test.IO.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {

    /*
    * 思路:
    * 1、应该有计数器,每次程序启动都需要计数一次
    * 2、计数器就是一个变量。突然冒出一个想法,程序启动时候进行计数,计数器必须存在于内存并进行运算。
    * 可是程序一结束,计数器就消失了。那么再次启动该程序,计数器又重新被初始化了。
    * 我们需要多次启动同一个应用程序,使用的是同一个计数器。
    * 这就需要计数器的声明周期变长,从内存存储到硬盘文件中。
    * 3、如何使用这个计数器呢?
    *   首先,程序启动时先读取这个用于记录计数器信息的配置文件。
    *   获取上一次的计数器次数,并进行次数的判断
    *   其次,对该次数进行自增,并自增后的次数进行重新存储到配置文件中。
    *   4、文件中的信息该如何存储并体现呢
    *   直接存储次数可以,但是不明确该数据的含义。所以起名字就变得很重要
    *   这就有了名字和值的对应,所以可以使用键值对。
    *   可是映射关系map集合搞定,又需要读取硬盘上的数据,所以map+i0
    *   =Properties
    * */

    public static void main(String[] args) throws IOException {
        getAppCount();
    }

    public static void getAppCount() throws IOException {
        //将配置文件封装成File对象
        File confile=new File("count.properties");

        if (!confile.exists()){
            confile.createNewFile();
        }

        FileInputStream fis=new FileInputStream(confile);

        Properties prop=new Properties();

        prop.load(fis);

        //从集合中通过键获取次数
        String value=prop.getProperty("time");

        //定义计数器。获取到的次数
        int count=0;


        if (value!=null){
            count=Integer.parseInt(value);
            if (count>=5){

                throw  new RuntimeException("使用次数已到,请注册,给钱!");
            }
        }
        count++;
        //将改变后的次数重新存储到硬盘中
        prop.setProperty("time",count+"");

        FileOutputStream fos=new FileOutputStream(confile);

        prop.store(fos,"");

        fos.close();

        fis.close();
    }

}

 

 

IO中的其他类

  打印流、序列流、操作对象

  PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。  

  

  序列流:SequenceInputStream对多个流进行合并。表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

  多个流是否合并,读-1后再判断是否有下一个流。直至没有下一个流,将最后一个流的-1作为最后的-1。这样就将多个源合并为一个源。

  

public class Pringt {

    public static void main(String[] args) throws IOException {


        /*
        * PrintWriter:字符打印流
        * 构造函数参数:
        * 1、字符串路径
        * 2、File对象
        * 3、字节输出流
        * 4、字符输出流
        * */

        Vector<FileInputStream> v=new Vector<FileInputStream>();

        v.add(new FileInputStream("1.txt"));
        v.add(new FileInputStream("2.txt"));
        v.add(new FileInputStream("3.txt"));

        Enumeration<FileInputStream> en=v.elements();

        SequenceInputStream sis=new SequenceInputStream(en);


        FileOutputStream fos=new FileOutputStream("4.txt");

        byte[] buf=new byte[1024];

        int len=0;

        while ((len=sis.read(buf))!=-1){
            fos.write(buf,0,len);
        }

        fos.close();
        sis.close();
    }
}

 Vector效率低

  

    public static void main(String[] args) throws IOException {


        /*
        * PrintWriter:字符打印流
        * 构造函数参数:
        * 1、字符串路径
        * 2、File对象
        * 3、字节输出流
        * 4、字符输出流
        * */

        ArrayList<FileInputStream> a1=new ArrayList<FileInputStream>();


        for (int x=1;x<=3;x++){
            a1.add(new FileInputStream(x+".txt"));
        }

        Enumeration<FileInputStream> en1=Collections.enumeration(a1);
        //枚举和迭代重复
        final Iterator<FileInputStream> it=a1.iterator();

        Enumeration<FileInputStream> en=new Enumeration<FileInputStream>() {
            @Override
            public boolean hasMoreElements() {
                return it.hasNext();
            }

            @Override
            public FileInputStream nextElement() {
                return it.next();
            }
        };

        SequenceInputStream sis=new SequenceInputStream(en);


        FileOutputStream fos=new FileOutputStream("4.txt");

        byte[] buf=new byte[1024];

        int len=0;

        while ((len=sis.read(buf))!=-1){
            fos.write(buf,0,len);
        }

        fos.close();
        sis.close();
    }

 

  

 

posted @ 2018-02-28 14:33  张小铁  阅读(146)  评论(0编辑  收藏  举报