반응형
[Java 실습 #12] 파일 입출력(IO) - 파일 경로 찾기 java.net.URL 활용(1) - 편집기(IDE) 에서 읽기
1. 편집기 (IDE - IntelliJ) 에서 실행
2. 개발환경
3. 구현
3.1. 프로젝트 구조
3.2. 텍스트 파일 - csv 형태
3.3. 구현 - 소스코드
4. 결과 화면 - IDE 콘솔창
1. 편집기 (IDE) 에서 실행
IntelliJ IDEA 2021.2 (Community Edition)
2. 개발환경
MacOS M1 - macOS Monterey 12.0.1
JDK 1.8
IntelliJ IDEA 2021.2 (Community Edition)
3. 구현
3.1. 프로젝트 구조
3.2. 텍스트 파일 - csv 형태
[ sample-data.txt ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
ID, FIRST NAME, LAST NAME, AGE, GENDER, EMAIL, SALARY
=====================================================
1001 , Michael , Jordan, 23, M, michael@tester.com , 30_000_000
1002 , Michelle , Jordan, 24, F, michelle@tester.com , 31_000_000
1003 , Adrian , Grey , 25, M , adrian@tester.com , 32_000_000
1004 , Adriana , Grey , 26, F , adriana@tester.com , 33_000_000
1005 , Harry , Potz , 27 , M , harry@tester.com , 34_000_000
1006 , Harriot , Potz , 28 , F , harriot@tester.com , 35_000_000
1007 , Mary , Granger , 29, F , mary@tester.com , , 36_000_000
1008 , Susan , Little , 53 , F , susan@tester.com , 37_000_000
1009 , Peter , Johnson , 30 , M , peter@tester.com , 38_000_000
1010 , Maria , Storm , 31 , F , maria@tester.com , 39_000_000
1011 , Helen , Hays , 32 , F , helen@tester.com , 40_000_000
1012 , Zeus , Nolan , 33 , M , zeus@tester.com , 41_000_000
1013 , Hector , Wood , 34 , M , hector@tester.com , 42_OOO_000
1014 , Chris , Munteanu, 35 , M , chris@tester.com , 43_000_000
1014 , Boris , Jackson , 36 , M , boris@tester.com , 44_000_000
1015 , Boris , Johnson , 36 , M , boris@tester.com , 44_000_000
1016 , Jack , Hope , 37 , M , jack@tester.com , 45_000_000
1017 , Janet , Hope , 37 , M , janet@tester.com , 46_000_000
|
3.3. 구현 - 소스 코드
java.net.URL 활용하여 파일 경로찾기
BufferedReader, FileReader 활용
[ App02.java ]
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
|
package io.home.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App02 {
private static URL url;
private static String filePath;
private static final String CLASS_PATH_NAME = App02.class.getName();
private static final String RESOURCE_PATH = "resource";
private static final String FILE_NAME = "sample-data.txt";
private static final String URL_FILE_PREFIX = "file:";
private static final String JAR_FILE_PREFIX = "jar:";
public static void main(String[] args) {
url = getUrl(CLASS_PATH_NAME, RESOURCE_PATH + "/" + FILE_NAME);
filePath = getFilePathFromURL(url);
if (!Files.exists(Paths.get(filePath))) {
filePath = getJarFilePath(filePath) + "/" + RESOURCE_PATH + "/" + FILE_NAME;
System.out.println("FILE PATH: " + filePath);
System.out.println("\nRun executable JAR!\n");
} else {
System.out.println("FILE PATH: " + filePath);
System.out.println("\nRun in IDE!\n");
}
if (filePath == null) {
System.out.println("file path not found!");
return;
}
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static URL getUrl(String className, String fileName) {
try {
Class<?> clazz = Class.forName(className);
ClassLoader classLoader = clazz.getClassLoader();
url = classLoader.getResource(fileName);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
assert url != null;
return url;
}
private static String getFilePathFromURL(URL url) {
String uriStr = null;
try {
uriStr = url.toURI().toString();
} catch (URISyntaxException use) {
use.printStackTrace();
}
assert uriStr != null;
String result = uriStr.startsWith(JAR_FILE_PREFIX) ? uriStr.substring((JAR_FILE_PREFIX + URL_FILE_PREFIX).length()) : uriStr.startsWith(URL_FILE_PREFIX) ? uriStr.substring(URL_FILE_PREFIX.length()) : uriStr;
System.out.println("URI STRING: " + result);
return result;
}
private static String getJarFilePath(String fullJarFilePath) {
char[] textChar = fullJarFilePath.toCharArray();
int len = textChar.length;
int rem = 0;
boolean flag = false;
for (int i = len - 1; i > 1; --i) {
if (textChar[i] == '!') {
flag = true;
}
if (textChar[i] == '/' && flag) {
rem = i;
break;
}
}
String jarFilePath = fullJarFilePath.substring(0, rem);
System.out.println("CURRENT JAR DIR: " + jarFilePath);
return jarFilePath;
}
}
|
4. 결과 화면 - IDE 콘솔창
- 결과
- IDE에서는 java.net.URL 활용하면 파일시스템에서 읽기 때문에 가능
반응형