简体   繁体   English

替换Properties.list

[英]Replacement for Properties.list

I need to convert Properties objects to byte[], I used 我需要将Properties对象转换为byte []

byte[] bytes = SerializationUtils.serialize(properties);

But it doesn't seem to be working well as all the data gets corrupted and I see some symbols and special charters. 但由于所有数据都被破坏,我看起来效果不佳,我看到了一些符号和特殊章程。 So I used the below approach 所以我使用了以下方法

StringWriter writer = new StringWriter();
properties.list(new PrintWriter(writer));
String fileContent = writer.getBuffer().toString();
byte[] bytes = fileContent.getBytes();

The above approach seems to work fine, but now calling properties.list is creating some issues as I was getting some text like "-- listing properties --" on the UI side, looks like Properties list method has hardcoded above string via out.println 上面的方法似乎工作正常,但现在调用properties.list创建一些问题,因为我在UI端获得了一些文本,如“列出属性 - ”,看起来像属性列表方法已经硬编码上面的字符串。的println

public void list(PrintWriter out) {
        out.println("-- listing properties --");
        Hashtable h = new Hashtable();
        enumerate(h);
        for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
            String key = (String)e.nextElement();
            String val = (String)h.get(key);
            if (val.length() > 40) {
                val = val.substring(0, 37) + "...";
            }
            out.println(key + "=" + val);
        }
    }

Is there any other better way achieve it or some replacement for Properties.list? 有没有其他更好的方法来实现它或一些替换Properties.list?

The first approach is fine. 第一种方法很好。

The point is that depending on what you want to do with that binary data, further encoding is required. 关键在于,根据您想要对二进制数据执行的操作,需要进一步编码。 For example, you could use Base64 encoding to turn your bytes into something that can be "moved" around easily. 例如,您可以使用Base64编码将您的字节转换为可以轻松“移动”的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM