Thinking in java 4th Edition 读书笔记-I/O(1)

1.The File class has a deceiving name; you might think it refers to a file, but it doesn’t. In fact, "FilePath" would have been a better name for the class. It can represent either the name of a particular file or the names of a set of files in a directory. If it’s a set of files, you can ask for that set using the list( ) method, which returns an array of String.
 
2.public interface FilenameFilter {
boolean accept(File dir, String name);
}

3.inner Class implement(not clear) Note that the argument to filter( ) must be final. This is required by the anonymous inner class so that it can use an object from outside its scope. This design is an improvement because the FilenameFilter class is now tightly bound to DirList2.

page 650 Exercise 2:

Create a class called SortedDirList with a constructor that takes a File object and builds a sorted directory list from the files at that File. Add to this class two overloaded list( ) methods: the first produces the whole list, and the second produces the subset of the list that matches its argument (which is a regular expression).

There may be several files named like "DirList.java" "Dictionary.java".

//{Args: "D.*/.java"} ???Begin with 'D' and end with '.java'

import java.util.regex.*;

import java.io.*;

import java.util.*;

class DirFilter implements FilenameFilter {

    private Pattern pattern;

    public DirFilter(String regex) {

        pattern = Pattern.compile(regex);

    }

    public boolean accept(File dir, String name) {

        return pattern.matcher(name).matches();

    }

}

public class SortedDirList {

    private String[] list;

    private File path;

    SortedDirList(File path){ 

        this.path=path; 

        list = path.list(); 

        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);

    } 

    public void print(){ 

        for(String dirItem : list)  

            System.out.println(dirItem);

    } 

    public String[] list(){ 

        return list;

    } 

    public String[] list(String reg){ 

        list = path.list(new DirFilter(reg)); 

        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); 

        return list;

    } //

    public static void main(String[] args) {

        File dir = new File("."); 

        SortedDirList list = new SortedDirList(dir); 

        System.out.println("///the first method"); 

        list.list(); 

        list.print(); 

        System.out.println("///the second method///"); 

        list.list("b.*"); 

        list.print();

    }

}

Page 650 Exercise 3:

Modify DirList.java (or one of its variants) so that it sums up the file sizes of the selected files.

//{Args: "D.*/.java"}

import java.util.regex.*;

import java.io.*;

import java.util.*;

public class DirList {

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

        File path = new File(".");

        String[] list;

        int size=0;

        if(args.length == 0)

            list = path.list();

        elselist = path.list(new DirFilter(args[0]));

        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);

        for(String dirItem : list){

            System.out.println(dirItem);

            InputStream temp = new FileInputStream(dirItem);

            size+=temp.available();//get the size

            temp.close();

        }

        System.out.println("the sum of size: "+size+" byte");//show the size

    }

}

class DirFilter implements FilenameFilter {

    private Pattern pattern;

    public DirFilter(String regex) {

        pattern = Pattern.compile(regex);

    }

    public boolean accept(File dir, String name) {

        return pattern.matcher(name).matches();

    }



source code for the book(used by Exercise 1,which I did not work out)

http://www.mindviewinc.com/TIJ4/CodeInstructions.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值