자바 간단 한 파일 관리자 만 들 기 (1)
12652 단어 자바
대학 4 학년 이 되 었 습 니 다. 대학 3 학년 이 끝 난 후에 한 국영 기업 의 연구 개발 팀 에 들 어가 일 을 했 습 니 다. 짧 은 두 달 동안 실습 한 시간 은 대부분 자신 이 공 부 했 습 니 다.최근 며칠 간 갑자기 졸 려 서 전에 만 든 것들 을 나 누 기로 했다.
목적 과 내용
대상 지향 프로 그래 밍 사상 을 활용 하여 자바 파일 관리 와 I / O 프레임 워 크 를 바탕 으로 그래 픽 인터페이스 기반 GUI 파일 관리 자 를 실현 합 니 다.1. 폴 더 생 성, 삭제, 진입 을 실현 합 니 다.2. 현재 폴 더 의 내용 나열 을 실현 합 니 다.3. 파일 복사 와 폴 더 복사 (폴 더 복사 란 깊이 복사, 모든 하위 디 렉 터 리 와 파일 포함) 를 실현 합 니 다.4. 지정 한 파일 의 암호 화 와 복호화 실현.5. 지정 한 파일 과 폴 더 의 압축 을 실현 합 니 다.6. 압축 파일 의 압축 해 제 를 실현 합 니 다.7. 파일 관리 자 는 그래 픽 인터페이스 가 있 습 니 다.
분석 하 다.
평소 에는 시스템 의 자원 관리 자 를 사용 하 는데 갑자기 스스로 쓰 면 당황 할 수 있다.그러나 단계별 분석 을 통 해 하나의 파일 관리 자 를 간략하게 분석 한 후에 맥락 이 매우 뚜렷 하 다 는 것 을 알 수 있다.실제로 당신 이 진정 으로 조작 하고 자 하 는 것 은 문자열 이 고 구체 적 인 파일 이나 폴 더 를 대표 하기 때 문 입 니 다.
어떻게 우리 가 조작 하고 자 하 는 경 로 를 얻 습 니까?
public interface I_Node {
//
public abstract Object getChild(String fileKind, int index);
//
public abstract Object getChildFile( int index) ;
//
public abstract Object getRoot();
//
public abstract int getChildCount(String fileKind);
//
public abstract String toString();
//
public abstract Icon getIcon();
//
public abstract boolean isLeaf();
//
public abstract String getPath();
//
public abstract Object getParent();
//
public abstract Object getCurrent();
//
public abstract void addChild(I_Node node);
//
public abstract File getFile();
//
public abstract void deleteChild(I_Node node);
//
public abstract long getSize();
//
public abstract void removeAllChildren();
}
public class FileNode implements I_Node {//
private Vector allFiles = new Vector();
private Vector folder = new Vector();
private FileSystemView fileSystemView = FileSystemView.getFileSystemView();
private File file = null;
private long dirLenth = 0;
public FileNode() {
file = fileSystemView.getHomeDirectory();
addChildren();
}
public FileNode(File file) {
this.file=file;
addChildren();
}
//
public void addChild(I_Node node) {
allFiles.add(node.getFile());
}
//
private void addChildren() {
File[] fileList = fileSystemView.getFiles(file, true);
for (File file : fileList) {
allFiles.add(file);
if (file.isDirectory()
&& !file.getName().toLowerCase().endsWith(".lnk")) {
folder.add(file);
}
}
}
//
public int getChildCount(String fileKind) {
if (fileKind.equals("files")) { //
return allFiles.size();
} else if (fileKind.equals("folder")) { //
return folder.size();
} else {
return 0;
}
}
//
public Object getChild(String fileKind, int index) {
if (fileKind.equals( "files")) {
return new FileNode(allFiles.get(index));
} else if (fileKind.equals("folder")) {
return new FileNode(folder.get(index));
} else {
return null;
}
}
//
public Object getChildFile( int index) {
if(indexreturn allFiles.get(index);
}
return null;
}
//
public Object getParent() {
return new FileNode(file.getParentFile());
}
//
public Object getRoot() {
return this;
}
//
public String toString() {
return fileSystemView.getSystemDisplayName(file);
}
//
public Icon getIcon() {
return fileSystemView.getSystemIcon(file);
}
//
public boolean isLeaf() {
return folder.size() == 0;
}
//
public String getPath() {
return this.file.getPath();
}
//
public Object getCurrent() {
return this;
}
//
public File getFile() {
return this.file;
}
//
public void deleteChild(I_Node node) {
allFiles.remove(node.getFile());
}
//
public long getSize() {
if (this.getFile().isDirectory()) {
return getDirSize(this.getFile().getPath());
} else {
return getFileSize();
}
}
//
private long getFileSize() {
return this.getFile().length();
}
//
private long getDirSize(String dir) {
File[] fileList = new File(dir).listFiles();
for (File file : fileList) {
if (file.isDirectory()) {
getDirSize(file.getPath());
} else {
dirLenth = dirLenth + file.length();
}
}
return dirLenth;
}
//
public void removeAllChildren() {
this.allFiles.removeAllElements();
}
}
미완이다
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.