Skip to content

Commit 1b18ca1

Browse files
author
Gary Gregory
committed
Add PathUtils.readString(Path, Charset).
1 parent 05176a7 commit 1b18ca1

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ The <action> type attribute can be add,update,fix,remove.
105105
<action dev="ggregory" type="add" due-to="Gary Gregory">
106106
Add NullReader.INSTANCE.
107107
</action>
108+
<action dev="ggregory" type="add" due-to="Gary Gregory">
109+
Add PathUtils.readString(Path, Charset).
110+
</action>
108111
<!-- UPDATE -->
109112
<action dev="ggregory" type="update" due-to="Dependabot">
110113
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.UncheckedIOException;
2424
import java.net.URI;
2525
import java.net.URL;
26+
import java.nio.charset.Charset;
2627
import java.nio.file.CopyOption;
2728
import java.nio.file.DirectoryStream;
2829
import java.nio.file.FileVisitOption;
@@ -56,6 +57,7 @@
5657
import java.util.stream.Collectors;
5758
import java.util.stream.Stream;
5859

60+
import org.apache.commons.io.Charsets;
5961
import org.apache.commons.io.IOExceptionList;
6062
import org.apache.commons.io.IOUtils;
6163
import org.apache.commons.io.file.Counters.PathCounters;
@@ -873,6 +875,20 @@ public static BasicFileAttributes readBasicFileAttributesUnchecked(final Path pa
873875
}
874876
}
875877

878+
/**
879+
* Reads the given path as a String.
880+
*
881+
* @param path The source path.
882+
* @param charset How to convert bytes to a String.
883+
* @return a new String
884+
* @throws IOException if an I/O error occurs reading from the stream
885+
* @see Files#readAllBytes(Path)
886+
* @since 2.12.0
887+
*/
888+
public static Object readString(final Path path, final Charset charset) throws IOException {
889+
return new String(Files.readAllBytes(path), Charsets.toCharset(charset));
890+
}
891+
876892
/**
877893
* Relativizes all files in the given {@code collection} against a {@code parent}.
878894
*

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.net.URI;
26+
import java.nio.charset.StandardCharsets;
2627
import java.nio.file.DirectoryStream;
2728
import java.nio.file.FileSystem;
2829
import java.nio.file.FileSystems;
@@ -34,6 +35,7 @@
3435
import java.util.Map;
3536

3637
import org.apache.commons.io.filefilter.NameFileFilter;
38+
import org.apache.commons.lang3.StringUtils;
3739
import org.junit.jupiter.api.Test;
3840
import org.junit.jupiter.api.io.TempDir;
3941

@@ -218,4 +220,19 @@ public void testNewDirectoryStream() throws Exception {
218220
}
219221
}
220222

223+
@Test
224+
public void testReadStringEmptyFile() throws IOException {
225+
final Path path = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
226+
assertEquals(StringUtils.EMPTY, PathUtils.readString(path, StandardCharsets.UTF_8));
227+
assertEquals(StringUtils.EMPTY, PathUtils.readString(path, null));
228+
}
229+
230+
@Test
231+
public void testReadStringSimpleUtf8() throws IOException {
232+
final Path path = Paths.get("src/test/resources/org/apache/commons/io/test-file-simple-utf8.bin");
233+
final String expected = "ABC\r\n";
234+
assertEquals(expected, PathUtils.readString(path, StandardCharsets.UTF_8));
235+
assertEquals(expected, PathUtils.readString(path, null));
236+
}
237+
221238
}

0 commit comments

Comments
 (0)