2014-11-09 2 views
1

자바에서 정규 표현식으로 작업이 있는데 질문이 있습니다.정규 표현식 java 속성에서 읽음

sentences = text.split(); 

어떻게 예를 들어

regexp.DECLARATIVE_SENTENCE = "\\."; 

파일 regexp.properties에서 정규 표현식()에 전달할 수 있습니다.

나는

public static final String DECLARATIVE_SENTENCE = "\\."; 

같은 상수를 사용하여보다

sentences = text.split(DECLARATIVE_SENTENCE); 

수 있도록하지만 난 속성 파일에서 인수를 읽을 필요가있다. 내가 어떻게 할 수 있니? 예를 들어 리소스 번들을 사용하고 있습니까?

답변

-2
import java.util.ResourceBundle; 

ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName); 
rb.getString(DECLARATIVE_SENTENCE) 
0

제대로 이해하고 있다면 속성 파일에서 정규식 값을로드하려고합니다.

이렇게하려면 Java 내에서 Properties이라는 클래스를 사용할 수 있습니다.

public Properties GetPropertiesFile(String location) throws IOException { 
    Properties prop = new Properties(); 
    FileInputStream inputStream = null; 

    try 
    { 
     inputStream = new FileInputStream(location); 
     prop.load(inputStream); 
    } finally { 
     if (inputStream != null) { 
     inputStream.close(); 
     } 
    } 

    return prop; 
} 

당신은 FileInputStream 함께 플레이해야 그리고 네 경우에 따라 getClass().getClassLoader().getResourceAsStream를 사용하지만, 이것은 당신이 가까이해야 할 수 있습니다. 당신은 속성 구성에 따라 위치를 파일을 변경해야하고, 등록 정보 파일은 자원 어쩌면 FileInputStream 경우) 할 수

Properties prop = GetPropertiesFile("regexp.properties"); 
String regex = prop.getProperty("DECLARATIVE_SENTENCE"); 

String[] ret = str.split(regex); 

: 여기

은 위의 기능을 사용하는 방법입니다.