λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
였λ₯˜ ν•΄κ²°

Tomcat μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ csv 파일 읽기

by syLim___ 2024. 7. 28.
728x90

βœ… ꡬ쑰

일단 macOS κΈ°μ€€ brew둜 μ„€μΉ˜ν•œ ν†°μΊ£ μ„œλ²„ κ΅¬μ‘°λŠ” μ΄λ ‡κ²Œ 생겼닀.

/opt
└── homebrew
    └── Cellar
        └── tomcat@9
            └── 9.0.91
                β”œβ”€β”€ bin
                └── libexec
                    └── webapps
                        └── lms1
                            └── index.jsp
                            └── WEB-INF
                                └── classes
                                    └── Student.class
                                    └── Department.class
                                    └── DepartmentManage.class
                        └── lms2
                            └── index.jsp
                            └── students.csv
                            └── WEB-INF
                                └── classes
                                    └── Student.class
                                    └── Department.class
                                    └── DepartmentManage.class

 

 

μž‘μ—… μ€‘μ΄λ˜ ν΄λ”λŠ” lms2이고,

DepartmentManage.classμ—μ„œ students.csv νŒŒμΌμ„ 읽으렀고 ν–ˆλŠ”λ°

계속 λͺ»μ½μ–΄μ„œ κ½€ 였래 μ‚½μ§ˆμ„ ν–ˆλ‹€.


νŒŒμΌμ„ λͺ» μ½μ—ˆλ˜ μ΄μœ λŠ”

DepartmentManage.class 파일이 싀행될 λ•Œ, νŒŒμΌμ„ μ½λŠ” λ””ν΄νŠΈ pathκ°€

 

`/opt/homebrew/Cellar/tomcat@9/9.0.91/libexec/webapps/lms2` ν•˜μœ„ μ–΄λ”˜κ°€μΌκ±°λΌκ³  μƒκ°ν–ˆλŠ”λ°

(μ •ν™•νžˆλŠ” `lms2/` , `WEB-INF/`, `WEB-INF/classes`, `WEB-INF/classes/students` 쀑에 ν•˜λ‚˜μΌ 거라고 μ˜ˆμƒν–ˆλŠ”λ°)

 

μ•Œκ³  λ³΄λ‹ˆ `/opt/homebrew/Cellar/tomcat@9/9.0.91/bin`μ΄μ—ˆλ‹€.

 

 

(System.getProperties("user.dir") 을 μ΄μš©ν•΄μ„œ ν˜„μž¬ μž‘μ—… 디렉토리λ₯Ό 좜λ ₯ν•΄λ³΄μ•˜λ”λ‹ˆ
.class파일의 μœ„μΉ˜κ°€ 좜λ ₯λ˜μ§€ μ•Šκ³ 
μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‹€ν–‰λ˜λŠ” μœ„μΉ˜μΈ /bin 폴더가 좜λ ₯λ˜λŠ” 것을 ν™•μΈν•˜μ˜€λ‹€.

 


 

ν•΄κ²° 방법은 두 가지이닀.

 

1. System.getProperties("user.dir") 둜 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰ 경둜λ₯Ό κ°€μ Έμ˜¨ λ’€, λ‚˜λ¨Έμ§€ 경둜 μž‘μ•„μ£ΌκΈ°

 

    System.getProperty("user.dir") -> `/opt/homebrew/Cellar/tomcat@9/9.0.91/bin`
    new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() -> `/opt/homebrew/Cellar/tomcat@9/9.0.91`
    filePath -> `/opt/homebrew/Cellar/tomcat@9/9.0.91/libexec/webapps/lms2/students.csv`

public class DepartmentManage {
    
    ...

    public void readCsv(String fileName) {

        String filePath = new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath() + 
            File.separator + "libexec" + File.separator + "webapps" + File.separator +
            "lms2" + File.separator + fileName;

        try(BufferedReader reader = new BufferedReader(new FileReader(filePath));) {
            String line;
            while((line = reader.readLine()) != null) {

                ... 
                
            }
            
        } catch(IOException e) {
            
        }
        
    }

}

 


2. μ‹€ν–‰μ‹œν‚€λŠ” .class 파일이 μžˆλŠ” 경둜λ₯Ό κ°€μ Έμ™€μ„œ, csv 파일 경둜 μž‘μ•„μ£ΌκΈ°

μ΄λ•ŒλŠ” νŒŒμΌμ„ μ°ΎκΈ° 쉽도둝, csv νŒŒμΌμ„ .class 파일과 같은 νŒ¨ν‚€μ§€μ— μœ„μΉ˜μ‹œμΌ°λ‹€.

public class DepartmentManage {
    
    ...

    public void readCsv(String fileName) {

        try(BufferedReader reader = new BufferedReader(
            new InputStreamReader(DepartmentManage.class.getResourceAsStream(fileName), "UTF-8"));
        ) {
           String line;
            while((line = reader.readLine()) != null) {

                ... 
                
            }
        } catch(IOException e) {
            
        }
    }

}
728x90