ProgramingTip

속성 파일에서 등호 기호를 이스케이프하는 방법

bestdevel 2020. 10. 11. 10:58
반응형

속성 파일에서 등호 기호를 이스케이프하는 방법


=Java 속성 파일에서 등호 ( )를 어떻게 이스케이프 할 수 있습니까? 내 파일에 다음과 같은 내용을 싶습니다.

table.whereclause=where id=100

또한 javadoc의 클래스에서 로드 (Reader Reader) 메소드를 참조하십시오.Property

에서 load(Reader reader)방법 문서가 말한다

열쇠는 최초의 비 공백 문자 및 최대 시작하는 줄의 문자를 모두 포함하고, 최초의 이스케이프를 포함하지 않는 '=', ':'또는 줄 끝 이외의 공백 문자를 포함합니다. 모든 필수 종료 문자는 선행 백 슬래시 문자로 이스케이프하여 키에 있습니다. 예를 들면

\:\=

두 문자 키가됩니다. ":=".줄 종결 자 문자는 \r\n이스케이프 시퀀스를 사용하여 사용할 수 있습니다 . 건너 뛰고 건너 뛰십시오. 키 뒤의 첫 번째 비 공백 문자가 '='또는 ':'이면 무시되고 그 뒤의 모든 공백 문자도 건너 뛰었습니다. 줄의 나머지 모든 문자는 요소의 일부가됩니다. 남은 문자가없는 경우 요소는 빈 곳 ""입니다. 키와 요소를 구성하는 원시 문자 시퀀스가 ​​식별 시퀀스에서 이스케이프 처리가 수행됩니다.

도움이 되셨기를 바랍니다.


특정 예에서는 등호를 이스케이프 할 필요가 없습니다. 키의 일부인 경우에만 이스케이프됩니다. 속성 파일 형식은 첫 번째 이스케이프 처리되지 않은 다음의 모든 문자를 값의 일부로 처리합니다.


Java의 기본 이스케이프 문자는 '\'입니다.
그러나 Java 속성 파일의 형식은 키 = 가치를 의미하는 첫 번째 단계 이후의 모든 값으로 전달됩니다.


이러한 종류의 문제를 방지하는 가장 좋은 방법은 프로그래밍 방식으로 속성을 빌드 한 다음 저장하는 것입니다. 예를 들어 다음과 같은 코드를 사용합니다.

java.util.Properties props = new java.util.Properties();
props.setProperty("table.whereclause", "where id=100");
props.store(System.out, null);

이스케이프 된 버전을 System.out에 출력합니다.

제 경우 출력은 다음과 가변합니다.

#Mon Aug 12 13:50:56 EEST 2013
table.whereclause=where id\=100

보시다시피는 파일의 내용을 쉽게 생성하는 방법입니다. 그리고 원하는만큼 많은 속성을 넣을 수 있습니다.


제 경우에는 두 개의 선행 '\\'가 잘 작동합니다.

예 : 단어에 '#'문자가 포함 된 경우 (예 : aa # 100) 두 개의 선행 '\\'로 이스케이프 할 수 있습니다.

   key= aa\\#100


여기에서 볼 수 있습니다 . Java 속성의 키에 공백 문자가 있습니까?

이스케이프 '='\ u003d

table.where 절 = 여기서 id = 100

key : [table.whereclause] 값 : [여기서 id = 100]

table.where 절 \ u003d 여기서 id = 100

키 : [table.whereclause = where] 값 : [id = 100]

table.whereclause \ u003dwhere \ u0020id \ u003d100

key : [table.whereclause = where id = 100] 값 : []


Spring 또는 Spring 부트에서 application.properties 파일은 특수 문자를 이스케이프하는 방법입니다.

table.whereclause = 여기서 id '\ ='100


메소드 .properties파일 과 100 % 호환되는 값을 프로그래밍 방식으로 생성하는 데 도움이 됩니다.

public static String escapePropertyValue(final String value) {
    if (value == null) {
        return null;
    }

    try (final StringWriter writer = new StringWriter()) {
        final Properties properties = new Properties();
        properties.put("escaped", value);
        properties.store(writer, null);
        writer.flush();

        final String stringifiedProperties = writer.toString();
        final Pattern pattern = Pattern.compile("(.*?)escaped=(.*?)" + Pattern.quote(System.lineSeparator()) + "*");
        final Matcher matcher = pattern.matcher(stringifiedProperties);

        if (matcher.find() && matcher.groupCount() <= 2) {
            return matcher.group(matcher.groupCount());
        }

        // This should never happen unless the internal implementation of Properties::store changed
        throw new IllegalStateException("Could not escape property value");
    } catch (final IOException ex) {
        // This should never happen. IOException is only because the interface demands it
        throw new IllegalStateException("Could not escape property value", ex);
    }
}

다음과 같이 부를 수 있습니다.

final String escapedPath = escapePropertyValue("C:\\Users\\X");
writeToFile(escapedPath); // will pass "C\\:\\\\Users\\\\X"

이 방법은 약간 비싸지 만 파일에 속성을 쓰는 것은 일반적으로 산발적 인 작업입니다.


문자 ": 안에 값을 입력 할 수있었습니다.

db_user="postgresql"
db_passwd="this,is,my,password"

참고 URL : https://stackoverflow.com/questions/2406975/how-to-escape-the-equals-sign-in-properties-files

반응형