_대문 | 방명록 | 최근글 | 홈피소개 | 주인놈
FrontPage › Java-계층구조로파일보기

hello world 말고 다른거 2번째로 console에 찍어본다. 1번째는 SQLj 프로그램이었고, 이게 2번째다. 뭐..일단... 재귀호출을 성공시켰으니.. 코드의 꾸짐은 그냥 패스~

c:\test 디렉토리가 존재한다는 가정...
import java.io.File;

class FileTree{
	String depth;
	int d;
	FileTree(){
		depth = "";
		d = 0;
	}
	public String replicate(String str, int sum){
		String rs = "";
		for(int i=1; i <= sum; i++){
			rs = rs + str;
		}
		return rs;
	}
	public void addDepth(){
		d++;
		depth = replicate("	", d);
	}
	public void minusDepth(){
		d--;
		depth = replicate("	", d);
	}
	
	public void TreeList(String path){
		File f1 = new File(path);
		String s[] = f1.list();
		for(int i=0;i<s.length;i++){
			File child_f = new File(path + "\\" + s[i]);
			if (child_f.isDirectory()){
					addDepth();
					System.out.println(depth + path + "\\" + s[i]);
					TreeList(path + "\\" + s[i]);
					minusDepth();
			}else{
				addDepth();
				System.out.println(depth + s[i]);
				minusDepth();
			}
			
		}
	}
}

public class FileDirDemo1 {
	public static void main(String args[]){
		
		FileTree ftree = new FileTree();
		System.out.println("C:\\test");
		ftree.TreeList("C:\\test");
		
	}
}

댓글 남기기..
이름: : 오른쪽의 새로고침을 클릭해 주세요. 새로고침
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:53

은혜를 입은 사람은 그 은혜를 마음속에 간직하고 있어야 하지만 은혜를 베푼 사람은 그것을 기억하고 있어서는 안 된다. (키케로)