จะเขียนค่าคุณสมบัติของภาษาต่าง ๆ ในรูปแบบดั้งเดิมไปยังไฟล์คุณสมบัติใน java โดยบันทึกเค้าโครงไฟล์ได้อย่างไร

ฉันมีไฟล์คุณสมบัติที่แปลเป็นภาษาท้องถิ่นพร้อมรายการคู่คีย์-ค่า ฉันต้องการเขียนค่าคุณสมบัติเป็นภาษาญี่ปุ่น จีน เยอรมัน ฯลฯ ลงในไฟล์ และยังต้องการบันทึกเค้าโครงไฟล์ที่มีอยู่แล้วพร้อมความคิดเห็นและการเว้นวรรค จำเป็นต้องเขียนภาษาเหล่านี้ในรูปแบบของตนเอง

ฉันพยายามเพิ่มคุณสมบัติใหม่ ("key = アカウント ナビゲーしョン CON ンポーネント") ให้กับไฟล์คุณสมบัติที่มีอยู่โดยใช้ PropertiesConfigurationLayout เป็นไปได้ที่จะเพิ่มคุณสมบัติใหม่ในรูปแบบดั้งเดิมและ PropertiesConfigurationLayout ช่วยในการบันทึกเค้าโครงของไฟล์ แต่รูปแบบคีย์ที่มีอยู่จะถูกเปลี่ยนเป็นรูปแบบยูนิโค้ด ลิงค์ที่มีประโยชน์ : (http://marjavamitjava.com/modifying-property-file-maintaining-order-well-comments/)

นี่คือรหัสที่ฉันลอง

รหัส:

    File file = new File("base_ch.properties");

    PropertiesConfiguration config = new PropertiesConfiguration();
    config.setEncoding("UTF-8");
    PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
    Properties props = new Properties();

    try(InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8"))
    {
        layout.load(in);
        OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(file), "UTF-8");

        props.put("key","アカウント ナビゲーション コンポーネント");
        layout.save(out);
        props.store(out, null);
    }
    catch (ConfigurationException | IOException e) {
        e.printStackTrace();
    }

base_ch.properties เนื้อหาไฟล์ก่อนรันโค้ด:

# -----------------------------------------------------------------------
#  All rights reserved.
#  Comments included here
# -----------------------------------------------------------------------
#Tue May 22 13:41:37

account.quote.expiration.time.label = Gültig bis
address.zipcode = 邮政编码:

base_ch.properties เนื้อหาไฟล์หลังจากการรันโค้ด:

# -----------------------------------------------------------------------
#  All rights reserved.
#  Comments included here
# -----------------------------------------------------------------------
#Tue May 22 13:41:37

account.quote.expiration.time.label = G\u00FCltig bis
address.zipcode = \u90AE\u653F\u7F16\u7801
#Wed Dec 06 17:40:04 IST 2017
key=アカウント ナビゲーション コンポーネント

ฉันต้องการบันทึกเค้าโครงไฟล์โดยไม่มีการเปลี่ยนแปลงใดๆ และควรคงคุณสมบัติที่มีอยู่ในรูปแบบดั้งเดิมไว้ สามารถเขียนภาษาต่างๆ ลงในไฟล์คุณสมบัติโดยใช้คลาสต่างๆ ได้ แต่เค้าโครงไฟล์จะมีการเปลี่ยนแปลงในกรณีดังกล่าว PropertiesConfigurationLayout เป็นวิธีเดียวที่ฉันพบในการบันทึกเค้าโครง

ใครสามารถช่วยฉันได้บ้าง?


person Nitz    schedule 06.12.2017    source แหล่งที่มา
comment
สิ่งนั้นเกิดขึ้นเนื่องจากไฟล์คุณสมบัติถูกเข้ารหัสตามค่าเริ่มต้นตามมาตรฐาน ISO-8859-1 ซึ่งไม่สามารถแสดงอักขระ Unicode ได้ทั้งหมด ดังนั้นยูนิโค้ดจึงหลบหนี   -  person Henry    schedule 06.12.2017
comment
ขอบคุณเฮนรี่ ใช่การเข้ารหัสเริ่มต้นคือ ISO-8859-1 แต่ฉันใช้ UTF-8 เพื่อเปลี่ยนการเข้ารหัสเริ่มต้นนั้น มีวิธีแก้ไขปัญหานี้หรือไม่?   -  person Nitz    schedule 06.12.2017


คำตอบ (2)


มีวิธีแก้ปัญหาสำหรับคำถาม

กำหนดคลาส PropertiesConfiguration และ PropertiesConfigurationLayout เองเพื่อให้ตรงตามข้อกำหนด

เขียนเมธอด escapeValue() ใหม่ใน PropertiesConfiguration ในลักษณะที่จะส่งคืนค่าคุณสมบัติโดยไม่ต้องใช้อักขระหลีก

แทนที่วิธี save ใน PropertiesConfigurationLayout

ด้านล่างนี้เป็นคลาสที่ขยาย PropertiesConfiguration :

    public class PropertiesConfigurationExtended extends PropertiesConfiguration{

    private static final char[] SEPARATORS = new char[] {'=', ':'};
    private static final char[] WHITE_SPACE = new char[]{' ', '\t', '\f'};
    private static final String ESCAPE = "\\";

    public static class PropertiesWriter extends PropertiesConfiguration.PropertiesWriter{
        private char delimiter;
        /**
         * Constructor.
         */
        public PropertiesWriter(Writer writer, char delimiter)
        {
            super(writer,delimiter);
            this.delimiter = delimiter;
        }
        public void writeProperty(String key, Object value,
                boolean forceSingleLine) throws IOException
        {
            String v;    
            if (value instanceof List)
            {
                List values = (List) value;
                if (forceSingleLine)
                {
                    v = makeSingleLineValue(values);
                }
                else
                {
                    writeProperty(key, values);
                    return;
                }
            }
            else
            {
                v = escapeValue(value);
            }

            write(escapeKey(key));
            write(" = ");
            write(v);

            writeln(null);
        }
        /**
         * Rewrite the escapeValue method to avoid escaping of the given property value.
         *
         * @param value the property value
         * @return the same property value
         */
        private String escapeValue(Object value)
        {
            return String.valueOf(value);
        }
    }
}

ด้านล่างนี้เป็นคลาสที่ขยาย PropertiesConfigurationLayout :

public class PropertiesConfigurationLayoutExtended extends PropertiesConfigurationLayout{

    public PropertiesConfigurationLayoutExtended(PropertiesConfigurationExtended config) {
        super(config);
    }
    public void save(Writer out) throws ConfigurationException
    {
        try
        {
            char delimiter = getConfiguration().isDelimiterParsingDisabled() ? 0
                    : getConfiguration().getListDelimiter();
            PropertiesConfigurationExtended.PropertiesWriter writer = new PropertiesConfigurationExtended.PropertiesWriter(
                    out, delimiter);
            if (getHeaderComment() != null)
            {
                writer.writeln(getCanonicalHeaderComment(true));
                writer.writeln(null);
            }

            for (Iterator it = getKeys().iterator(); it.hasNext();)
            {
                String key = (String) it.next();
                if (getConfiguration().containsKey(key))
                {
                    // Output blank lines before property
                    for (int i = 0; i < getBlancLinesBefore(key); i++)
                    {
                        writer.writeln(null);
                    }
                    // Output the comment
                    if (getComment(key) != null)
                    {
                        writer.writeln(getCanonicalComment(key, true));
                    }
                    // Output the property and its value
                    boolean singleLine = (isForceSingleLine() || isSingleLine(key))
                            && !getConfiguration().isDelimiterParsingDisabled();
                    writer.writeProperty(key, getConfiguration().getProperty(
                            key), singleLine);
                }
            }
            writer.flush();
        }
        catch (IOException ioex)
        {
            throw new ConfigurationException(ioex);
        }
    }
}

คลาสสำหรับเขียนคุณสมบัติลงในไฟล์:

    File file = new File("base_ch.properties");
    PropertiesConfigurationExtended config = new PropertiesConfigurationExtended();
    PropertiesConfigurationLayoutExtended layout = new PropertiesConfigurationLayoutExtended(config);

    try(InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8"))
    {
        layout.load(in);    
        OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        config.setProperty("key","アカウント ナビゲーション コンポーネント");
        layout.save(out, false));       
    }
    catch (ConfigurationException | IOException e) {
        e.printStackTrace();
    }
person Nitz    schedule 07.12.2017

หากเป็นเพียงการเพิ่มคุณสมบัติให้กับไฟล์ที่มีอยู่ คุณก็สามารถเพิ่มมันได้:

    Properties props = new Properties();
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("file", true), "UTF-8");
    props.put("key", "アカウント ナビゲーション コンポーネント");
    props.store(out, null);
person Evgeniy Dorofeev    schedule 06.12.2017
comment
ขอบคุณ Evgeniy Dorofeev สำหรับการตอบกลับของคุณ รหัสนี้จะเขียนทับไฟล์คุณสมบัติด้วยคุณสมบัติใหม่นั้น ข้อกำหนดของฉันไม่เพียงแต่เกี่ยวกับการเพิ่มคุณสมบัติเท่านั้น แต่ยังต้องบันทึกเค้าโครงไฟล์ด้วย - person Nitz; 07.12.2017