개요
프로젝트 중 현재 날짜를 기반으로 해당 기간에 해당되는 JSP를 호출할 필요가 있었다.
하지만 임시로 사용되는 기능이었기 때문에 DB를 사용할 수 없었다.
그래서 JSP 네이밍 패턴을 아래와 같이 작성한 후 현재 날짜가 시작일과 마감일 사이라면 해당 JSP를 호출하도록 하려고했다.
- {시작일}_{마감일}{이름}.jsp
- 0101_0131JspView.jsp
특정 경로의 파일 목록을 어떻게 가져 올 수 있을까?
java.io.File
Java에서는 File 클래스를 통해서 파일과 디렉토리를 제어할 수 있다. 파일 생성, 읽기, 수정 및 삭제 등 여러가지 기능을 제공한다.
내가 필요한건 java 프로젝트의 jsp 디렉토리에 있는 jsp 파일 이름들이 필요하다.
import java.io.File;
public class FileExplorer {
public static void main(String[] args) {
String path = System.getProperty("user.dir");
File file = new File(path, "src/main/webapp/jsp");
String[] jspFileNameArr = file.list();
System.out.printf("path : %s%n", file);
if(jspFileNameArr != null) {
for(String jspFileName : jspFileNameArr) {
System.out.printf("jspFileName : %s%n", jspFileName);
}
}
}
}
- System.getProperty("user.dir") : System.getProperty() 메서드는 자바를 실행할 때 실행되는 곳의 정보를 얻어오거나 운영체제의 정보를 제공해준다. "user.dir"은 현재 실행중인 디렉토리 경로를 제공해준다. 즉, Java 프로그램이 실행되는 디렉토리 경로를 반환하게 된다. 이 밖에도 유용한 기능이 있으니 링크를 참고해보자 (참고 링크)
- new File(path, "src/main/webapp/jsp") : File 객체를 생성할 때 인자로 경로를 전달해주면 해당 경로의 파일 또는 디렉토리의 file 객체가 반환된다. 지금은 java 프로그램의 디렉토리 경로(parent)와 jsp파일이 있는 세부 경로(child)를 인자로 전달해주었다. 이렇게 전달해주면 File 객체에서 해당인자를 하나의 경로로 이어준다.
- file.list() : 해당 디렉토리에 있는 파일 및 디렉토리의 이름을 배열로 반환해준다. 이 밖에도 다른 메서드들도 있다.
- list(FilenameFilter filter) : 필터링 조건에 맞는 파일 이름 목록 반환
- listFiles() : 파일 목록 반환
- listFiles(FilenameFilter filter) : 필터링 조건에 맞는 파일 목록 반환
콘솔에 출력된 내용을 확인해보자
path : /Users/green-bin/Desktop/code/file-practice/src/main/webapp/jsp
jspFileName : 0101_0131JspView.jsp
jspFileName : 0211_0229JspView.jsp
jspFileName : 0201_0210JspView.jsp
추가로 listFiles()로 출력했을 경우 아래와 같이 나온다.
path : /Users/green-bin/Desktop/code/file-practice/src/main/webapp/jsp
jspFileName : /Users/green-bin/Desktop/code/file-practice/src/main/webapp/jsp/0101_0131JspView.jsp
jspFileName : /Users/green-bin/Desktop/code/file-practice/src/main/webapp/jsp/0211_0229JspView.jsp
jspFileName : /Users/green-bin/Desktop/code/file-practice/src/main/webapp/jsp/0201_0210JspView.jsp
참조
File (Java Platform SE 8 )
Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the path
docs.oracle.com
System Properties (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
'Java' 카테고리의 다른 글
Java - JVM 구조와 동작 방식 (1) | 2024.11.03 |
---|---|
Mybatis - SAXParseException: The content of elements must consist of well-formed character data or markup 원인과 해결법 (0) | 2024.06.05 |
Eclipse Memory Analyzer - Eclipse MAT 설치 방법 (MacOS) (0) | 2023.08.17 |
Java - @JasonCreator로 DTO에서 유연하게 Enum Type 받기 (0) | 2023.04.26 |
Java - 커스텀 애너테이션으로 유효성 검사하기 (0) | 2023.04.22 |