我正在使用这段代码:
String v="bla";
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer updated =encoder.encode(CharBuffer.wrap(v+"albalb"));
转换工作正常,我成功将此ByteBuffer
打印到位于我的云中的文件。现在,我想在v打印到文件后新行。我尝试过那些东西:
\r\n
\n
public static String newline = System.getProperty("line.separator");
他们都不适合我。有人有想法吗?
答案 0 :(得分:0)
以下代码似乎对我有用。
String v = "Hello";
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer updated =encoder.encode(CharBuffer.wrap(v+"\nWorld!"));
String s = new String(updated.array(), "UTF-8");
System.out.println(s);
也许错误在别的地方?
关于评论的编辑。
//Client side encoding.
String inputString = "Hello\nWorld!";
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
byte[] encodedBytes = Base64.getEncoder().encode(inputString.getBytes());
ByteBuffer updated = encoder.encode(CharBuffer.wrap(new String(encodedBytes, charset)));
String s = new String(updated.array(), charset);
System.out.println("Base64 encoded string: " + s);
//Server side decoding Base64
CharsetDecoder decoder = charset.newDecoder();
CharBuffer decodedBytes = decoder.decode(updated);
byte[] decoded = Base64.getDecoder().decode(decodedBytes.toString().getBytes());
String decodedStr = new String(decoded, charset);
System.out.println("Base64 decoded string: " + decodedStr);