Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IO-842] deprecate writeLines methods without a charset #563

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -3297,7 +3297,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 @@ -3313,7 +3315,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 @@ -3328,7 +3332,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 @@ -3345,7 +3351,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 @@ -3363,7 +3371,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 @@ -3381,7 +3389,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 @@ -3402,7 +3410,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 @@ -3421,7 +3429,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
4 changes: 1 addition & 3 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3876,9 +3876,7 @@ public static void writeLines(final Collection<?> lines, String lineEnding, fina
* @param charsetName the name of the requested charset, null means platform default
* @throws NullPointerException if the output is null
* @throws IOException if an I/O error occurs
* @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
* .UnsupportedEncodingException} in version 2.2 if the
* encoding is not supported.
* @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported
* @since 1.1
*/
public static void writeLines(final Collection<?> lines, final String lineEnding,
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 @@ -3160,6 +3160,14 @@ public void testWriteLinesEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousF
assertEquals(expected, actual);
}

@Test
public 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
public void testWriteStringToFile_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception {
final File file = TestUtils.newFile(tempDirFile, "lines.txt");
Expand Down