Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -238,6 +238,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add and use RandomAccessFileMode.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -141,9 +141,9 @@ private RelativeSortedPaths(final Path dir1, final Path dir2, final int maxDepth
}
}
- private static final OpenOption[] OPEN_OPTIONS_TRUNCATE = new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING};
+ private static final OpenOption[] OPEN_OPTIONS_TRUNCATE = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING};
- private static final OpenOption[] OPEN_OPTIONS_APPEND = new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.APPEND};
+ private static final OpenOption[] OPEN_OPTIONS_APPEND = {StandardOpenOption.CREATE, StandardOpenOption.APPEND};
/**
* Empty {@link CopyOption} array.
@@ -1394,6 +1394,26 @@ public static boolean waitFor(final Path file, final Duration timeout, final Lin
.filter(path -> pathFilter.accept(path, readAttributes ? readBasicFileAttributesUnchecked(path) : null) == FileVisitResult.CONTINUE);
}
+ /**
+ * Writes the given character sequence to a file at the given path.
+ *
+ * @param path The target file.
+ * @param charSequence The character sequence text.
+ * @param charset The Charset to encode the text.
+ * @param openOptions options How to open the file.
+ * @return The given path.
+ * @throws IOException if an I/O error occurs writing to or creating the file.
+ * @since 2.12.0
+ */
+ public static Path writeString(final Path path, final CharSequence charSequence, final Charset charset, final OpenOption... openOptions)
+ throws IOException {
+ // Check the text is not null before opening file.
+ Objects.requireNonNull(path, "path");
+ Objects.requireNonNull(charSequence, "charSequence");
+ Files.write(path, String.valueOf(charSequence).getBytes(Charsets.toCharset(charset)), openOptions);
+ return path;
+ }
+
/**
* Does allow to instantiate.
*/

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import org.apache.commons.io.filefilter.NameFileFilter;
+import org.apache.commons.io.test.TestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
@@ -256,4 +258,12 @@ private Path writeToNewOutputStream(final boolean append) throws IOException {
return file;
}
+ @Test
+ public void testWriteStringToFile1() throws Exception {
+ final Path file = tempDir.resolve("write.txt");
+ PathUtils.writeString(file, "Hello /u1234", StandardCharsets.UTF_8);
+ final byte[] text = "Hello /u1234".getBytes(StandardCharsets.UTF_8);
+ TestUtils.assertEqualContent(text, file);
+ }
+
}

MODIFY
src/test/java/org/apache/commons/io/test/TestUtils.java
src/test/java/org/apache/commons/io/test/TestUtils.java
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
* @throws IOException If an I/O error occurs while reading the file contents
*/
public static void assertEqualContent(final byte[] b0, final File file) throws IOException {
+ assertEqualContent(b0, file.toPath());
+ }
+
+ /**
+ * Assert that the content of a file is equal to that in a byte[].
+ *
+ * @param b0 the expected contents
+ * @param file the file to check
+ * @throws IOException If an I/O error occurs while reading the file contents
+ */
+ public static void assertEqualContent(final byte[] b0, final Path file) throws IOException {
int count = 0, numRead = 0;
final byte[] b1 = new byte[b0.length];
- try (InputStream is = Files.newInputStream(file.toPath())) {
+ try (InputStream is = Files.newInputStream(file)) {
while (count < b0.length && numRead >= 0) {
numRead = is.read(b1, count, b0.length);
count += numRead;
@@ -74,9 +86,20 @@ public static void assertEqualContent(final byte[] b0, final File file) throws I
* @throws IOException If an I/O error occurs while reading the file contents
*/
public static void assertEqualContent(final char[] c0, final File file) throws IOException {
+ assertEqualContent(c0, file.toPath());
+ }
+
+ /**
+ * Assert that the content of a file is equal to that in a char[].
+ *
+ * @param c0 the expected contents
+ * @param file the file to check
+ * @throws IOException If an I/O error occurs while reading the file contents
+ */
+ public static void assertEqualContent(final char[] c0, final Path file) throws IOException {
int count = 0, numRead = 0;
final char[] c1 = new char[c0.length];
- try (Reader ir = Files.newBufferedReader(file.toPath())) {
+ try (Reader ir = Files.newBufferedReader(file)) {
while (count < c0.length && numRead >= 0) {
numRead = ir.read(c1, count, c0.length);
count += numRead;

Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).