Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3363,7 +3363,9 @@ public static void writeByteArrayToFile(final File file, final byte[] data, fina
* @param lines the lines to write, {@code null} entries produce blank lines
* @throws IOException in case of an I/O error
* @since 1.3
* @deprecated use {@link #writeLines(File, String, Collection)} instead (and specify the appropriate encoding)
*/
@Deprecated
public static void writeLines(final File file, final Collection<?> lines) throws IOException {
writeLines(file, null, lines, null, false);
}
Expand All @@ -3379,7 +3381,9 @@ public static void writeLines(final File file, final Collection<?> lines) throws
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
* @since 2.1
* @deprecated use {@link #writeLines(File, String, Collection, boolean)} instead (and specify the appropriate encoding)
*/
@Deprecated
public static void writeLines(final File file, final Collection<?> lines, final boolean append) throws IOException {
writeLines(file, null, lines, null, append);
}
Expand All @@ -3394,7 +3398,9 @@ public static void writeLines(final File file, final Collection<?> lines, final
* @param lineEnding the line separator to use, {@code null} is system default
* @throws IOException in case of an I/O error
* @since 1.3
* @deprecated use {@link #writeLines(File, String, Collection, String)} instead (and specify the appropriate encoding)
*/
@Deprecated
public static void writeLines(final File file, final Collection<?> lines, final String lineEnding) throws IOException {
writeLines(file, null, lines, lineEnding, false);
}
Expand All @@ -3411,7 +3417,9 @@ public static void writeLines(final File file, final Collection<?> lines, final
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
* @since 2.1
* @deprecated use {@link #writeLines(File, String, Collection, boolean)} instead and specify the appropriate encoding
*/
@Deprecated
public static void writeLines(final File file, final Collection<?> lines, final String lineEnding, final boolean append) throws IOException {
writeLines(file, null, lines, lineEnding, append);
}
Expand All @@ -3426,7 +3434,7 @@ public static void writeLines(final File file, final Collection<?> lines, final
* @param charsetName the name of the requested charset, {@code null} means platform default
* @param lines the lines to write, {@code null} entries produce blank lines
* @throws IOException in case of an I/O error
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
* @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM
* @since 1.1
*/
public static void writeLines(final File file, final String charsetName, final Collection<?> lines) throws IOException {
Expand All @@ -3444,7 +3452,7 @@ public static void writeLines(final File file, final String charsetName, final C
* @param append if {@code true}, then the lines will be added to the
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
* @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM
* @since 2.1
*/
public static void writeLines(final File file, final String charsetName, final Collection<?> lines, final boolean append) throws IOException {
Expand All @@ -3462,7 +3470,7 @@ public static void writeLines(final File file, final String charsetName, final C
* @param lines the lines to write, {@code null} entries produce blank lines
* @param lineEnding the line separator to use, {@code null} is system default
* @throws IOException in case of an I/O error
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
* @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM
* @since 1.1
*/
public static void writeLines(final File file, final String charsetName, final Collection<?> lines, final String lineEnding) throws IOException {
Expand All @@ -3481,7 +3489,7 @@ public static void writeLines(final File file, final String charsetName, final C
* @param append if {@code true}, then the lines will be added to the
* end of the file rather than overwriting
* @throws IOException in case of an I/O error
* @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
* @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM
* @since 2.1
*/
public static void writeLines(final File file, final String charsetName, final Collection<?> lines, final String lineEnding, final boolean append)
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/apache/commons/io/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,14 @@ void testWriteLinesEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLine
assertEquals(expected, actual);
}

@Test
void testWriteLinesUnsupportedCharset() throws Exception {
final File file = TestUtils.newFile(tempDirFile, "lines.txt");

final List<String> lines = Arrays.asList("my first line", "The second Line");
assertThrows(UnsupportedCharsetException.class, () -> FileUtils.writeLines(file, "there_is_no_such_charset", lines));
}

@Test
void testWriteStringToFile_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception {
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
Expand Down
Loading