-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsvReader.java
More file actions
59 lines (47 loc) · 1.85 KB
/
CsvReader.java
File metadata and controls
59 lines (47 loc) · 1.85 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
package life.mosu.mosuserver.application.admin.util;
import life.mosu.mosuserver.application.admin.dto.ApplicationCsvInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class CsvReader {
private static final String CSV_DELIMITER = ",";
public List<ApplicationCsvInfo> read(MultipartFile file) {
List<ApplicationCsvInfo> results = new ArrayList<>();
if (file == null || file.isEmpty()) {
log.warn("업로드된 파일이 비어있습니다.");
return results;
}
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8))) {
String line;
boolean isHeader = true;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (line.isBlank()) continue;
if (isHeader) {
isHeader = false;
continue;
}
try {
String[] values = line.split(CSV_DELIMITER, -1);
results.add(ApplicationCsvInfo.of(values));
} catch (Exception e) {
log.error("CSV 파싱 실패 (Line {}): {}", lineNumber, e.getMessage());
throw new RuntimeException("CSV 파싱 오류");
}
}
} catch (Exception e) {
log.error("파일 읽기 실패: {}", e.getMessage(), e);
throw new RuntimeException("CSV 읽기 오류");
}
return results;
}
}