0

我目前正在为“概率和统计”课程做一些作业,关于磁盘段的大小和效率。

我写了简单的文件爬虫来收集数据

public static void addTree(File file, Collection<File> all) {
    File[] children = file.listFiles();
    if (children != null) {
        for (File child : children) {
            addTree(child, all);
        }
    }else{
        all.add(file);
    }
}
public static void main(String[] args) {
    Collection<File> all = new ArrayList<File>();
    //build file list
    System.out.println("COLLECTING");
    addTree(new File("."), all);
    //result file
    try {
        //result file stream
        FileWriter fstream = null;
        fstream = new FileWriter("result.txt");
        //result file writer
        BufferedWriter out = new BufferedWriter(fstream);

        System.out.println("SAVING STARTED");

        //loop - find size and extension (lowercase)
        Iterator itr = all.iterator();
        while(itr.hasNext()){
            //get file
            File tested = (File) itr.next();
            //get ext
            String[] splitted = tested.getName().split("\\.");
            String ext = splitted[splitted.length-1];
            //get size
            long size = tested.length();
            //put into file
            //if(size!=0){
                out.write(size+" "+ext);
                out.newLine();
            //}
        }
        //close file / save
        out.close();
    }catch(IOException ex){}
}

我有两个问题:1)扫描我的 D:/ 后,它显示爬虫发现了大约 480k 文件,但系统声称大约有 507k 文件。好的,我虽然可能是隐藏文件或类似的东西。但是在扫描 C:/ 之后,分数有点相反 - 爬虫发现 229k 而 windows 声称 227k。这让我有点难以理解,怎么会发生这样的事情。

2)如您所见

//if(size!=0){
    out.write(size+" "+ext);
    out.newLine();
//}

爬虫获取所有文件(甚至是零大小的文件),它出现了,我的 D:/ 上只有大约 104k 非零文件 od 507k (不是操作系统分区 - 只有应用程序文件 - 没有系统文件相关),在我看来是什么80% 的文件完全没有内容是荒谬的。我明白了,有时需要创建文件以供以后使用,但这看起来像是有人将文件名误解为 OS 全局变量或什么。

有人可以澄清这个问题吗?

4

0 回答 0