개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.07.28 02:28

ResourceBundle::create 관련 질문내용

목록
  • 코드주술사 6시간 전 2025.07.28 02:28 새글
  • 4
    1
제가 현재 Java의 국제화와 리소스 관리에 대해 공부 중인데,
ResourceBundle 클래스의 create 메서드에 대한 이해가 부족합니다.
ResourceBundle::create 메서드의 파라미터로 InputStream 객체를 넘기면
어떻게 동작하는지 궁금합니다.
InputStream 객체를 넘기면 어떻게 리소스 파일을 읽어들이는지,
또한 다른 파라미터를 넘기면 어떻게 동작하는지 궁금합니다.
해당 클래스의 메서드 설명을 참고했지만,
어떻게 동작하는지 구체적인 예제를 통해 이해할 수 있는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  6시간 전



    ResourceBundle 클래스의 create 메서드는 리소스 파일을 읽어들이기 위한 메서드입니다.

    InputStream 객체를 넘기면, 해당 객체가 가리키는 리소스 파일을 읽어들입니다.

    리소스 파일은 properties나 XML 형식으로 작성된 파일이어야 합니다.

    create 메서드의 파라미터 중 하나인 "name"은 리소스 파일의 이름을 지정합니다.

    이름을 지정하지 않으면, create 메서드는 리소스 파일을 찾지 못하고 Exception을 발생시킵니다.

    create 메서드의 파라미터 중 하나인 "locale"은 리소스 파일의 지역 설정을 지정합니다.

    지역 설정을 지정하지 않으면, create 메서드는 기본 지역 설정을 사용합니다.

    create 메서드의 파라미터 중 하나인 "ClassLoader"은 리소스 파일을 찾을 때 사용할 클래스 로더를 지정합니다.

    클래스 로더를 지정하지 않으면, create 메서드는 기본 클래스 로더를 사용합니다.

    예를 들어, properties 파일을 읽어들이기 위해 create 메서드를 호출할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle bundle = ResourceBundle.getBundle("myResources", Locale.KOREA);
    
    


    이 코드는 "myResources.properties" 파일을 읽어들입니다.

    만약 InputStream 객체를 넘기면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    InputStream inputStream = getClass().getResourceAsStream("/myResources.properties");
    
    ResourceBundle bundle = ResourceBundle.getBundle("", new PropertiesReader(inputStream));
    
    


    이 코드는 InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    PropertiesReader 클래스는 InputStream 객체를 통해 properties 파일을 읽어들이는 클래스입니다.

    #hostingforum.kr
    java
    
    public class PropertiesReader extends Properties {
    
        public PropertiesReader(InputStream inputStream) {
    
            try {
    
                load(inputStream);
    
            } catch (IOException e) {
    
                throw new RuntimeException(e);
    
            }
    
        }
    
    }
    
    


    이 클래스는 InputStream 객체를 통해 properties 파일을 읽어들이고, Properties 객체를 반환합니다.

    create 메서드의 파라미터 중 하나인 "ClassLoader"을 지정하지 않으면, create 메서드는 기본 클래스 로더를 사용합니다.

    만약 클래스 로더를 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ClassLoader classLoader = getClass().getClassLoader();
    
    ResourceBundle bundle = ResourceBundle.getBundle("", new PropertiesReader(inputStream), classLoader);
    
    


    이 코드는 클래스 로더를 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "locale"을 지정하지 않으면, create 메서드는 기본 지역 설정을 사용합니다.

    만약 지역 설정을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    Locale locale = Locale.KOREA;
    
    ResourceBundle bundle = ResourceBundle.getBundle("", new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 지역 설정을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "name"을 지정하지 않으면, create 메서드는 리소스 파일을 찾지 못하고 Exception을 발생시킵니다.

    만약 이름을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    String name = "myResources";
    
    ResourceBundle bundle = ResourceBundle.getBundle(name, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 이름을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "parentBundle"을 지정하지 않으면, create 메서드는 기본 부모 리소스 파일을 사용합니다.

    만약 부모 리소스 파일을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle parentBundle = ResourceBundle.getBundle("parentResources", locale);
    
    ResourceBundle bundle = ResourceBundle.getBundle("", new PropertiesReader(inputStream), classLoader, locale, parentBundle);
    
    


    이 코드는 부모 리소스 파일을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "baseName"을 지정하지 않으면, create 메서드는 기본 이름을 사용합니다.

    만약 이름을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    String baseName = "myResources";
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 이름을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "parentBundle"을 지정하지 않으면, create 메서드는 기본 부모 리소스 파일을 사용합니다.

    만약 부모 리소스 파일을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle parentBundle = ResourceBundle.getBundle("parentResources", locale);
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale, parentBundle);
    
    


    이 코드는 부모 리소스 파일을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "locale"을 지정하지 않으면, create 메서드는 기본 지역 설정을 사용합니다.

    만약 지역 설정을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    Locale locale = Locale.KOREA;
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 지역 설정을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "parentBundle"을 지정하지 않으면, create 메서드는 기본 부모 리소스 파일을 사용합니다.

    만약 부모 리소스 파일을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle parentBundle = ResourceBundle.getBundle("parentResources", locale);
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale, parentBundle);
    
    


    이 코드는 부모 리소스 파일을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "baseName"을 지정하지 않으면, create 메서드는 기본 이름을 사용합니다.

    만약 이름을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    String baseName = "myResources";
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 이름을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "parentBundle"을 지정하지 않으면, create 메서드는 기본 부모 리소스 파일을 사용합니다.

    만약 부모 리소스 파일을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle parentBundle = ResourceBundle.getBundle("parentResources", locale);
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale, parentBundle);
    
    


    이 코드는 부모 리소스 파일을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "locale"을 지정하지 않으면, create 메서드는 기본 지역 설정을 사용합니다.

    만약 지역 설정을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    Locale locale = Locale.KOREA;
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 지역 설정을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "parentBundle"을 지정하지 않으면, create 메서드는 기본 부모 리소스 파일을 사용합니다.

    만약 부모 리소스 파일을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    ResourceBundle parentBundle = ResourceBundle.getBundle("parentResources", locale);
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale, parentBundle);
    
    


    이 코드는 부모 리소스 파일을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라미터 중 하나인 "baseName"을 지정하지 않으면, create 메서드는 기본 이름을 사용합니다.

    만약 이름을 지정하고 싶다면, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    java
    
    String baseName = "myResources";
    
    ResourceBundle bundle = ResourceBundle.getBundle(baseName, new PropertiesReader(inputStream), classLoader, locale);
    
    


    이 코드는 이름을 지정하고, InputStream 객체를 통해 "myResources.properties" 파일을 읽어들입니다.

    create 메서드의 파라

    2025-07-28 02:29

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 39,482건 / 2 페이지

검색

게시물 검색