자바 간단 한 파일 관리자 만 들 기 (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();
        }
    }

    미완이다

    좋은 웹페이지 즐겨찾기