2017-03-15 5 views
0

でバイト配列に変換し、しかし、私は次のコードで行うことができますが、Propertiesオブジェクトは、私はプロパティが[]バイトをオブジェクトに変換するJava

private byte[] getBytes(Properties properties){ 
    StringWriter stringWriter = new StringWriter(); 
    PrintWriter printWriter=new PrintWriter(stringWriter); 
    properties.list(printWriter); 
    String fileContent = stringWriter.getBuffer().toString(); 
    byte[] bytes = fileContent.getBytes(); 
    try{ 
     stringWriter.close(); 
     printWriter.close(); 
    }catch (IOException e){ 
     log.error("unable to close resource stringWriter" + e.getStackTrace()); 
    } 

    return bytes; 
} 

しかしproperties.list(PrintWriterの)、印刷されます文字列 "--listing properties--"をコンソールに表示します。最善の方法を見つけるのを助けが必要です。

答えて

0

私はByteArrayOutputStreamを使ってPropertiesオブジェクトを変換しました。あなたの機能は次のように変更することができます:

private byte[] getBytes(Properties properties){ 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    try { 
     props.store(byteArrayOutputStream, ""); 
    } catch (IOException e) { 
     log.error("An error occurred while storing properties to a byte array: " + e.getStackTrace()); 
    } 

    return byteArrayOutputStream.toByteArray(); 
} 
関連する問題