You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/05/06 12:45:01 UTC
(commons-io) branch master updated: Handling null charset in FileUtils.readLines(File, Charset) (#744)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new a894c74c9 Handling null charset in FileUtils.readLines(File, Charset) (#744)
a894c74c9 is described below
commit a894c74c91dd09513f9271c986429d7a0eef9f79
Author: Ryan Kurtz <ry...@users.noreply.github.com>
AuthorDate: Tue May 6 08:44:55 2025 -0400
Handling null charset in FileUtils.readLines(File, Charset) (#744)
---
src/main/java/org/apache/commons/io/FileUtils.java | 2 +-
.../java/org/apache/commons/io/FileUtilsTest.java | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index e04977670..c29399435 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -2817,7 +2817,7 @@ public static List<String> readLines(final File file) throws IOException {
* @since 2.3
*/
public static List<String> readLines(final File file, final Charset charset) throws IOException {
- return Files.readAllLines(file.toPath(), charset);
+ return Files.readAllLines(file.toPath(), Charsets.toCharset(charset));
}
/**
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index b46a55f88..661c41288 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -2839,6 +2839,25 @@ public void testReadLines_Errors() {
assertThrows(UnsupportedCharsetException.class, () -> FileUtils.readLines(tempDirFile, "unsupported-charset"));
}
+ @Test
+ public void testReadLines_Defaults() throws Exception {
+ final File file = TestUtils.newFile(tempDirFile, "lines.txt");
+ try {
+ final String[] data = {"hello", "this is", "some text"};
+ TestUtils.createLineFileUtf8(file, data);
+
+ final List<String> lines1 = FileUtils.readLines(file);
+ final List<String> lines2 = FileUtils.readLines(file, (Charset) null);
+ final List<String> lines3 = FileUtils.readLines(file, Charset.defaultCharset());
+
+ assertEquals(lines1, Arrays.asList(data));
+ assertEquals(lines1, lines2);
+ assertEquals(lines1, lines3);
+ } finally {
+ TestUtils.deleteFile(file);
+ }
+ }
+
@Test
@EnabledIf("isPosixFilePermissionsSupported")
public void testReadLines_IOExceptionOnPosixFileSystem() throws Exception {