Bagaimana cara menulis nilai properti dari berbagai bahasa dalam bentuk aslinya ke file properti di java dengan menyimpan tata letak file?

Saya memiliki file properti yang dilokalkan dengan daftar pasangan nilai kunci. Saya ingin menulis nilai properti dalam bahasa Jepang, Cina, Jerman, dll. ke file dan juga ingin menyimpan tata letak file yang sudah ada dengan komentar dan spasi. Perlu menulis bahasa-bahasa ini dalam bentuk aslinya.

Saya mencoba menambahkan properti baru ("key = アカウント ナビゲーション コンポーネント") ke file properti yang ada menggunakan PropertiesConfigurationLayout. Dimungkinkan untuk menambahkan properti baru dalam bentuk aslinya dan PropertiesConfigurationLayout membantu menyimpan tata letak file. Namun format kunci yang ada akan diubah menjadi format unicode. Tautan yang berguna: (http://marjavamitjava.com/modifying-property-file-maintaining-order-well-comments/)

Ini adalah kode yang saya coba.

Kode:

    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 mengarsipkan konten sebelum kode dijalankan:

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

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

Konten file base_ch.properties setelah kode dijalankan:

# -----------------------------------------------------------------------
#  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=アカウント ナビゲーション コンポーネント

Saya ingin menyimpan tata letak file tanpa perubahan apa pun dan harus mempertahankan properti yang ada dalam bentuk aslinya. Ia dapat menulis berbagai bahasa ke file properti menggunakan berbagai kelas, tetapi tata letak file akan berubah dalam kasus tersebut. PropertiesConfigurationLayout adalah satu-satunya cara yang saya temukan untuk menyimpan tata letak.

Ada yang bisa bantu saya?


person Nitz    schedule 06.12.2017    source sumber
comment
Hal ini terjadi karena file properti secara default dikodekan dalam ISO-8859-1 yang tidak dapat mewakili semua karakter Unicode. Oleh karena itu, unicode lolos.   -  person Henry    schedule 06.12.2017
comment
Terima kasih Henry. Ya, pengkodean default adalah ISO-8859-1, tetapi saya menggunakan UTF-8 untuk mengubah pengkodean default itu. Apakah ada cara untuk mengatasi masalah ini?   -  person Nitz    schedule 06.12.2017


Jawaban (2)


Punya solusi untuk pertanyaan itu.

Sesuaikan kelas PropertiesConfiguration dan PropertiesConfigurationLayout untuk memenuhi persyaratan.

Tulis ulang metode escapeValue() di PropertiesConfiguration sedemikian rupa untuk mengembalikan nilai properti itu sendiri tanpa karakter escape.

Ganti metode simpan di PropertiesConfigurationLayout.

Di bawah ini diberikan adalah kelas yang memperluas 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);
        }
    }
}

Di bawah ini diberikan adalah kelas yang memperluas 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);
        }
    }
}

Kelas untuk menulis properti ke file:

    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

Jika ini hanya tentang menambahkan properti ke file yang ada maka Anda bisa menambahkannya:

    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
Terima kasih Evgeniy Dorofeev atas balasan Anda. Kode ini akan menimpa file properti dengan properti baru tersebut. Persyaratan saya bukan hanya tentang menambahkan properti, juga perlu menyimpan tata letak file. - person Nitz; 07.12.2017