论坛首页 Java企业应用论坛

一道java英文试题

浏览 7201 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-01-31  
package com.javiserrano.dir;

import java.io.File;
import java.util.List;

public class CommonFileFilter extends StatisticsFilter {

	@Override
	protected List<ExceptFile> sort(List<ExceptFile> efiles, File file) {

		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File expectfile : files) {
				if (expectfile.isDirectory()) {
					sort(efiles, expectfile);
				} else {
					ExceptFile efile = new ExceptFile();
					efile.setCapacity(expectfile.length());
					efile.setName(expectfile.getName());
					efile.setBelongDir(file.getAbsolutePath());
					efiles.add(efile);
				}
			}
		} else {
			ExceptFile efile = new ExceptFile();
			efile.setCapacity(file.length());
			efile.setName(file.getName());
			efile.setBelongDir(file.getAbsolutePath());
			efiles.add(efile);
		}
		return efiles;
	}
}
个人觉得递归的方式也许比较坑爹点,但是又想不出来其它,不知道有没有其它设计方案,设计理念就是过滤器方式
1 请登录后投票
   发表时间:2014-02-20  
小虾米也实现一下,大侠们轻拍

//实体类
package com.eking.functionFile;

public class SimpleFile implements Comparable{
private String url;
private String name;
private int length;

public SimpleFile() {
super();
}
public SimpleFile(String url, String name, int length) {
super();
this.url = url;
this.name = name;
this.length = length;
}

        //省略getter/setter

@Override
public int hashCode(){
return length*7+31;
}
@Override
public String toString() {
return url+name+"\t\t\t\t"+length;
}
public int compareTo(Object o) {
SimpleFile other = (SimpleFile)o;
if(this.length>other.getLength()){
return 1;
}else if(this.length<other.getLength()){
return -1;
}
return 0;
}
}

//结果类
package com.eking.functionFile;

import java.io.File;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Result {
private Set<SimpleFile> result = new TreeSet<SimpleFile>();
private int threshold = 0;
private final int num = 10;

public void addFile(File file){
if(result.size()<num){
int fileLength = Integer.parseInt(String.valueOf(file.length()));
result.add(new SimpleFile(file.getPath(),file.getName(),fileLength));
}else if(threshold<file.length()){
result.add(new SimpleFile(file.getPath(),file.getName(),Integer.parseInt(String.valueOf(file.length()))));
this.removeSmallest();
}
}

public void removeSmallest(){
Iterator<SimpleFile> iter = this.getResult().iterator();
iter.next();
iter.remove();
}
public Set<SimpleFile> getResult(){
return this.result;
}
}

//功能实现类
package com.eking.functionFile;

import java.io.File;
import java.util.Iterator;

public class ProcessFile {
private String direct = "d:/test";
private Result result = new Result();

public void getTopTenFile(){
this.getTopTenFile(direct);
}

public void getTopTenFile(String direct){
File file = new File(direct);
this.getTopTenFile(file);
}

public void getTopTenFile(File file){
if(file.isFile()){
result.addFile(file);
}else if(file.isDirectory()){
File[] subFiles = file.listFiles();
for(int i=0;i<subFiles.length;i++){
this.getTopTenFile(subFiles[i]);
}
}
}

public static void main(String[] args){
ProcessFile pf = new ProcessFile();
pf.getTopTenFile();
Iterator<SimpleFile> iter = pf.result.getResult().iterator();
while(iter.hasNext()){
System.out.println(iter.next().toString());
}
}
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics