
ResourceBundle::get 메서드는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
java
ResourceBundle bundle = ResourceBundle.getBundle("myBundle", locale);
String message = bundle.getString("hello");
Resource Bundle을 사용할 때는 properties 파일을 만드는 것이 일반적입니다. properties 파일을 만들기 위해서는 다음과 같은 방법을 사용할 수 있습니다.
1. 프로젝트의 src/main/resources 폴더에 properties 파일을 생성합니다.
2. properties 파일을 생성한 후, 해당 파일의 이름을 Bundle.properties로 변경합니다.
3. properties 파일에 키와 값을 추가합니다. 예를 들어, hello=Hello World
#hostingforum.kr
properties
hello=Hello World
Resource Bundle을 사용할 때는 locale을 지정할 수 있습니다. locale을 지정하는 방법은 다음과 같습니다.
#hostingforum.kr
java
Locale locale = new Locale("ko", "KR");
ResourceBundle bundle = ResourceBundle.getBundle("myBundle", locale);
String message = bundle.getString("hello");
위의 코드에서 locale을 지정하지 않았을 때, 기본적으로 사용되는 locale은 시스템의 locale입니다.
ResourceBundle::get 메서드는 throws Exception을 가질 수 있습니다. 예외처리를 다음과 같이 할 수 있습니다.
#hostingforum.kr
java
try {
ResourceBundle bundle = ResourceBundle.getBundle("myBundle", locale);
String message = bundle.getString("hello");
} catch (MissingResourceException e) {
System.out.println("해당 키가 없습니다.");
}
위의 코드에서 bundle.getString(\"hello\")을 호출할 때, \"hello\"이 없을 경우 MissingResourceException이 발생합니다. 예외를 처리하는 방법은 위의 예제와 같습니다.
위의 코드에서 bundle.getString(\"hello\")을 호출할 때, \"hello\"이 없을 경우 기본값을 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
java
String message = bundle.getString("hello", "기본값");
위의 코드에서 bundle.getString(\"hello\")을 호출할 때, \"hello\"이 없을 경우 기본값을 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
java
String message = bundle.getString("hello", "기본값");
위의 코드에서 bundle.getString(\"hello\")을 호출할 때, \"hello\"이 없을 경우 기본값을 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
java
String message = bundle.getString("hello", "기본값");
위의 코드에서 bundle.getString(\"hello\")을 호출할 때, \"hello\"이 없을 경우 기본값을 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
java
String message = bundle.getString("hello", "기본값");
2025-04-29 00:19