-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLs.java
More file actions
107 lines (97 loc) · 5.03 KB
/
Ls.java
File metadata and controls
107 lines (97 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package src;
import java.io.File;
import java.util.Objects;
import java.util.Scanner;
public class Ls {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("명령어를 입력하세요 (예: ls): ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("ls")) {
try {
// 컴파일된 .class 파일의 디렉토리 경로 설정
String classPath = System.getProperty("user.dir") + "/out/production/YourProjectName";
System.out.println("classPath: " + classPath);
File packageDir = new File(classPath);
System.out.println("현재 경로: " + packageDir.getAbsolutePath());
File[] files = packageDir.listFiles();
if (files != null) {
for (File file : files) {
System.out.println("파일: " + file.getName());
}
} else {
System.out.println("디렉토리에 파일이 없습니다.");
}
if (packageDir.exists() && packageDir.isDirectory()) {
System.out.println("현재 디렉토리: " + packageDir.getAbsolutePath());
for (File file : Objects.requireNonNull(packageDir.listFiles((dir, name) -> name.endsWith(".class")))) {
String className = file.getName().replace(".class", "");
System.out.println(className);
}
} else {
System.out.println("패키지에서 디렉토리를 찾을 수 없습니다.");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("invalid command. use 'ls' to list class.");
}
}
}
//package src;
//
//import java.io.File;
//import java.util.Objects;
//import java.util.Scanner;
//
//public class Ls {
// public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// System.out.print("명령어를 입력하세요 (예: ls): ");
// String input = scanner.nextLine();
//
// if (input.equalsIgnoreCase("ls")) {
// //equalsIgnoreCase는 대소문자 상관 없이 비교하게 해줌
// try {
// String classPath = Ls.class.getProtectionDomain().getCodeSource().getLocation().getPath();
// /*
// ls.class는 .class파일을 통해 클래스의 정보에 접근할 수 있음
// getProtectionDomain() 메서드는 클래스의 보안 영역을 가져옴
// getCodeSource()는 클래스가 로드된 코드 소스 정보를 가져오며 클래스가 로드된 위치를 나타냄(파일 경로)
// getLocation()은 코드 소스의 URL 객체를 반환함. URL은 파일이 저장된 디렉토리 위치를 나타냄
// getPath()는 URL 객체에서 경로를 추출해 문자열로 변환
// */
// File packageDir = new File(classPath);
// //실행경로 파악 가능, 이전에 얻은 실행위치를 기반으로 이 값을 file 객체에 전달해 경로를 기반으로 조작 가능
// System.out.println("classPath: " + classPath);
//
// if (packageDir.exists() && packageDir.isDirectory()) {
// //exists()sms 파일이나 디렉토리가 실제로 존재하는지 확인, isDirectory는 packageDir이 디렉토리인지 확인
// for (File file : Objects.requireNonNull(packageDir.listFiles((dir, name) -> name.endsWith(".class")))) {
// /*
// 디렉토리 안에 있는 파일 중 .class. 확장자로 끝나는 파일들만 필터링하여 배열로 반환하는 역할
// dir은 현재 탐색 중인 디렉토리를 나타내는 file 객체
// name은 해당 디렉토리에 있는 파일 또는 디렉토리의 이름
// endsWith은 .class로 끝나는 지 확인 해주는 듯
// */
// String className = file.getName().replace(".class", "");
// /*
// file.getName() 객체의 이름을 반환해줌
// .replace(".class", "") .class를 빈칸으로 대체
// */
// System.out.println(className);
// }
// } else {
// System.out.println("패키지에서 디렉토리를 찾을 수 없습니다.");
// }
// } catch (Exception e) {
// e.printStackTrace();
// //예외가 발생한 클래스, 메서드, 라인 번호를 포함한 상세 정보를 출력해줌
// }
// } else {
// System.out.println("invalid command. use 'ls' to list class.");
// }
// //scanner.close();
// }
//}