Код: Выделить всё
/**
* Prints {@code hello, world}, to the {@link System#out}, followed by a system dependent line separator.
*
* @param args command line arguments.
*/
public static void main(String... args) {
System.out.printf("hello, world%n"); // %n !!!!!!
}
Код: Выделить всё
/**
* Verifies that the {@link HelloWorld#main(String...)} method prints {@code hello, world}, to the
* {@link System#out}, followed by a system-dependent line separator.
*
* @author Jin Kwon <onacit_at_gmail.com>
* @see System#lineSeparator()
*/
@DisplayName("main(args) prints 'hello, world' followed by a system-dependent line separator")
@Test
public void main_PrintHelloWorld_() {
final var out = System.out;
try {
// --------------------------------------------------------------------------------------------------- given
final var buffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(buffer));
// ---------------------------------------------------------------------------------------------------- when
HelloWorld.main();
// ---------------------------------------------------------------------------------------------------- then
final var actual = buffer.toByteArray();
final var expected = ("hello, world" + System.lineSeparator()).getBytes(StandardCharsets.US_ASCII);
Assertions.assertArrayEquals(actual, expected);
} finally {
System.setOut(out);
}
}
Я не думаю, что неправильно предполагать, что < em>Системно-зависимый разделитель строк кодируется с помощью US_ASCII.
Подходит ли Charset#defaultCharset() для %n< /код>?
Подробнее здесь: https://stackoverflow.com/questions/790 ... eseparator