자바(JAVA FILE, DIRECTORY)에서 해당컴퓨터의 드라이브와 폴더수 파일수를 출력하는 프로그램
오라클자바커뮤니티에서 설립한 개발자교육6년차 오엔제이프로그래밍 실무교육센터
(오라클SQL,튜닝,힌트,자바프레임워크,안드로이드,아이폰,닷넷 실무개발강의)
import java.io.*;
import java.util.*;
/**
*
* @작성자 :정기욱
* 마지막 수정일자 2008. 10. 12
* @Description : 현재 컴퓨터 자원의 폴더개수와 파일개수를 체크하는 클래스
*/
public class FileSearch {
static StringBuffer lengths=new StringBuffer();
private String oneroot ="C:\"; //default root
private final String field="\";
private ArrayList fileList = new ArrayList();
private ArrayList dirList = new ArrayList();
private int fCount=0; //파일의 총개수
private int dCount=0; //디렉토리의 총개수
FileWriter fw;
//private
private int tmpdcount=0; //디렉토리 카운터
private int tmpdfcount=0; //파일 카운터
private HashMap resource = new HashMap();
private File [] root;
public FileSearch() throws IOException {
root = File.listRoots(); //현재컴퓨터의 루트디렉토리 정보를 저장함
}
public FileSearch(String sroot) { //원하는 디렉토리의 정보만 검색한다.
this.oneroot=sroot;
}
public void search(File DirName,String longpath) throws IOException {
try{
Vector dir=new Vector();
if(DirName!=null){
File [] tmp =DirName.listFiles();
if(tmp==null) return;
for(int i=0; i<tmp.length; i++) {
if(tmp[i].isDirectory()) {
tmpdcount++;
dCount++;
echo(tmp[i].getName());
dir.addElement(longpath+"\"+tmp[i].getName());
}
else{
tmpdfcount++;
fCount++;
echo(tmp[i].getName());
//echo(lengths.toString()+tmp[i].getName());
//fw.write(lengths.toString()+tmp[i].getName()+"\r\n");
}
}
Iterator iter=dir.iterator();
while(iter.hasNext()){
if(tmpdcount==0) return;
String tmpPath=(String)iter.next()+"\";
search(new File(tmpPath),tmpPath);
}
}else {
return;
}
}catch(NullPointerException ex) {
ex.printStackTrace();
}
}
public static void echo(String str){
System.out.println(str);
}
public void call() throws IOException { //현재root 디렉토리부터의 모든 자원을 점검한다.
for(int k=0; k<root.length; k++){
String tmproot=root[k].getAbsolutePath();
System.out.println(tmproot);
if(tmproot=="C:"")
System.exit(0);
if( (!root[k].getPath().equals("C:\")) && (!root[k].getPath().equals("D:\")) ) {
//시간관계상 용량이 많아서 조건을 걸어줬슴.
File dir3 = new File(tmproot);
File[] f = dir3.listFiles();
if(f!=null){
for(int i=0; i<f.length; i++) {
if(f[i].isDirectory()){
dirList.add(f[i].getName());
dCount++;
}else if(f[i].isFile()){
fileList.add(f[i].getName());
fCount++;
}
//echo(f[i].getName());
}
for(int i=0; i<dirList.size(); i++) {
String path="";
echo((String)dirList.get(i));
path=tmproot+(String)dirList.get(i);
echo(path);
search(new File(path),path);
}
//"디렉토리의 수:"+search.getDCount()+"총파일 수"+search.getFCount()
echo("=====");
}
resource.put(root[k].getAbsolutePath()+" 디렉토리의 수:"+dCount+"총파일 수"+fCount,root[k].getAbsolutePath());
//echo(root[k].getAbsolutePath()+" 디렉토리의 수:"+dCount+"총파일 수"+fCount+"\r\n");
dCount=0;fCount=0;
}
}
}
public int getFCount() {
return fCount;
}
public void setFCount(int count) {
fCount = count;
}
public int getDCount() {
return dCount;
}
public void setDCount(int count) {
dCount = count;
}
public HashMap getResource() {
return resource;
}
public static void main(String[] args) throws IOException {
FileSearch search = new FileSearch();
search.call();
Iterator iter = search.getResource().keySet().iterator();
while(iter.hasNext()) {
echo((String)(iter.next()));
}
}
}
import java.util.*;
/**
*
* @작성자 :정기욱
* 마지막 수정일자 2008. 10. 12
* @Description : 현재 컴퓨터 자원의 폴더개수와 파일개수를 체크하는 클래스
*/
public class FileSearch {
static StringBuffer lengths=new StringBuffer();
private String oneroot ="C:\"; //default root
private final String field="\";
private ArrayList fileList = new ArrayList();
private ArrayList dirList = new ArrayList();
private int fCount=0; //파일의 총개수
private int dCount=0; //디렉토리의 총개수
FileWriter fw;
//private
private int tmpdcount=0; //디렉토리 카운터
private int tmpdfcount=0; //파일 카운터
private HashMap resource = new HashMap();
private File [] root;
public FileSearch() throws IOException {
root = File.listRoots(); //현재컴퓨터의 루트디렉토리 정보를 저장함
}
public FileSearch(String sroot) { //원하는 디렉토리의 정보만 검색한다.
this.oneroot=sroot;
}
public void search(File DirName,String longpath) throws IOException {
try{
Vector dir=new Vector();
if(DirName!=null){
File [] tmp =DirName.listFiles();
if(tmp==null) return;
for(int i=0; i<tmp.length; i++) {
if(tmp[i].isDirectory()) {
tmpdcount++;
dCount++;
echo(tmp[i].getName());
dir.addElement(longpath+"\"+tmp[i].getName());
}
else{
tmpdfcount++;
fCount++;
echo(tmp[i].getName());
//echo(lengths.toString()+tmp[i].getName());
//fw.write(lengths.toString()+tmp[i].getName()+"\r\n");
}
}
Iterator iter=dir.iterator();
while(iter.hasNext()){
if(tmpdcount==0) return;
String tmpPath=(String)iter.next()+"\";
search(new File(tmpPath),tmpPath);
}
}else {
return;
}
}catch(NullPointerException ex) {
ex.printStackTrace();
}
}
public static void echo(String str){
System.out.println(str);
}
public void call() throws IOException { //현재root 디렉토리부터의 모든 자원을 점검한다.
for(int k=0; k<root.length; k++){
String tmproot=root[k].getAbsolutePath();
System.out.println(tmproot);
if(tmproot=="C:"")
System.exit(0);
if( (!root[k].getPath().equals("C:\")) && (!root[k].getPath().equals("D:\")) ) {
//시간관계상 용량이 많아서 조건을 걸어줬슴.
File dir3 = new File(tmproot);
File[] f = dir3.listFiles();
if(f!=null){
for(int i=0; i<f.length; i++) {
if(f[i].isDirectory()){
dirList.add(f[i].getName());
dCount++;
}else if(f[i].isFile()){
fileList.add(f[i].getName());
fCount++;
}
//echo(f[i].getName());
}
for(int i=0; i<dirList.size(); i++) {
String path="";
echo((String)dirList.get(i));
path=tmproot+(String)dirList.get(i);
echo(path);
search(new File(path),path);
}
//"디렉토리의 수:"+search.getDCount()+"총파일 수"+search.getFCount()
echo("=====");
}
resource.put(root[k].getAbsolutePath()+" 디렉토리의 수:"+dCount+"총파일 수"+fCount,root[k].getAbsolutePath());
//echo(root[k].getAbsolutePath()+" 디렉토리의 수:"+dCount+"총파일 수"+fCount+"\r\n");
dCount=0;fCount=0;
}
}
}
public int getFCount() {
return fCount;
}
public void setFCount(int count) {
fCount = count;
}
public int getDCount() {
return dCount;
}
public void setDCount(int count) {
dCount = count;
}
public HashMap getResource() {
return resource;
}
public static void main(String[] args) throws IOException {
FileSearch search = new FileSearch();
search.call();
Iterator iter = search.getResource().keySet().iterator();
while(iter.hasNext()) {
echo((String)(iter.next()));
}
}
}
오라클자바커뮤니티 추천 개발자 과정 - 개인80%환급
댓글 없음:
댓글 쓰기