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 010299c  Add PathUtils.writeString(Path, CharSequence, Charset, 
OpenOption...).
010299c is described below

commit 010299c21811b11a41c8d5bd73023f75eb29058e
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Sun Sep 26 14:46:35 2021 -0400

    Add PathUtils.writeString(Path, CharSequence, Charset, OpenOption...).
---
 src/changes/changes.xml                            |  3 +++
 .../java/org/apache/commons/io/file/PathUtils.java | 24 +++++++++++++++++--
 .../org/apache/commons/io/file/PathUtilsTest.java  | 10 ++++++++
 .../java/org/apache/commons/io/test/TestUtils.java | 27 ++++++++++++++++++++--
 4 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 107874b..971ff45 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index f9968c2..06131a2 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -141,9 +141,9 @@ public final class PathUtils {
         }
     }
 
-    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.
@@ -1395,6 +1395,26 @@ public final class PathUtils {
     }
 
     /**
+     * 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.
      */
     private PathUtils() {
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java 
b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index 63f29bd..410f030 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 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;
@@ -37,6 +38,7 @@ import java.util.Iterator;
 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 @@ public class PathUtilsTest extends TestArguments {
         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);
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/test/TestUtils.java 
b/src/test/java/org/apache/commons/io/test/TestUtils.java
index fe56b19..fc0c3a9 100644
--- a/src/test/java/org/apache/commons/io/test/TestUtils.java
+++ b/src/test/java/org/apache/commons/io/test/TestUtils.java
@@ -33,6 +33,7 @@ import java.io.Reader;
 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;
@@ -52,9 +53,20 @@ public abstract class TestUtils {
      * @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 abstract class TestUtils {
      * @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;

Reply via email to