Refactor internals into new methods:
Add FileUtils.newOutputStream(File, boolean).
Add PathUtils.newOutputStream(Path, boolean).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -114,6 +114,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add PathUtils.readString(Path, Charset).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add FileUtils.newOutputStream(File, boolean).
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add PathUtils.newOutputStream(Path, boolean).
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -2364,6 +2364,20 @@ public static void moveToDirectory(final File src, final File destDir, final boo
}
}
+ /**
+ * Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
+ * to the file.
+ *
+ * @param append Whether or not to append.
+ * @param file the File.
+ * @return a new OutputStream.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static OutputStream newOutputStream(final File file, final boolean append) throws IOException {
+ return PathUtils.newOutputStream(file.toPath(), append);
+ }
+
/**
* Opens a {@link FileInputStream} for the specified file, providing better error messages than simply calling
* {@code new FileInputStream(file)}.
@@ -2581,6 +2595,7 @@ public static String readFileToString(final File file, final String charsetName)
return readLines(file, Charsets.toCharset(charsetName));
}
+
private static void requireAbsent(final File file, final String name) throws FileExistsException {
if (file.exists()) {
throw new FileExistsException(
@@ -2588,7 +2603,6 @@ private static void requireAbsent(final File file, final String name) throws Fil
}
}
-
/**
* Throws IllegalArgumentException if the given files' canonical representations are equal.
*
@@ -3206,6 +3220,8 @@ public static void write(final File file, final CharSequence data, final Charset
write(file, data, charset, false);
}
+ // Private method, must be invoked will a directory parameter
+
/**
* Writes a CharSequence to a file creating the file if it does not exist.
*
@@ -3222,8 +3238,6 @@ public static void write(final File file, final CharSequence data, final Charset
writeStringToFile(file, Objects.toString(data, null), charset, append);
}
- // Private method, must be invoked will a directory parameter
-
/**
* Writes a CharSequence to a file creating the file if it does not exist.
*
@@ -3256,6 +3270,8 @@ public static void write(final File file, final CharSequence data, final String
write(file, data, Charsets.toCharset(charsetName), append);
}
+ // Must be called with a directory
+
/**
* Writes a byte array to a file creating the file if it does not exist.
* <p>
@@ -3272,8 +3288,6 @@ public static void writeByteArrayToFile(final File file, final byte[] data) thro
writeByteArrayToFile(file, data, false);
}
- // Must be called with a directory
-
/**
* Writes a byte array to a file creating the file if it does not exist.
*
@@ -3357,6 +3371,7 @@ public static void writeLines(final File file, final Collection<?> lines, final
writeLines(file, null, lines, null, append);
}
+
/**
* Writes the {@code toString()} value of each item in a collection to
* the specified {@code File} line by line.
@@ -3373,7 +3388,6 @@ public static void writeLines(final File file, final Collection<?> lines, final
writeLines(file, null, lines, lineEnding, false);
}
-
/**
* Writes the {@code toString()} value of each item in a collection to
* the specified {@code File} line by line.

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
@@ -833,6 +835,22 @@ public static boolean isRegularFile(final Path path, final LinkOption... options
return Files.newDirectoryStream(dir, new DirectoryStreamFilter(pathFilter));
}
+ /**
+ * Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
+ * to the file.
+ * @param path the Path.
+ * @param append Whether or not to append.
+ *
+ * @return a new OutputStream.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static OutputStream newOutputStream(final Path path, final boolean append) throws IOException {
+ return Files.newOutputStream(path, append ?
+ new OpenOption[] {StandardOpenOption.APPEND} :
+ new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
+ }
+
/**
* Returns true if the given options contain {@link StandardDeleteOption#OVERRIDE_READ_ONLY}.
*

MODIFY
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
-import java.nio.file.Files;
-import java.nio.file.OpenOption;
-import java.nio.file.StandardOpenOption;
import java.util.Objects;
import org.apache.commons.io.Charsets;
@@ -67,9 +64,7 @@ private static Writer initWriter(final File file, final Object encoding, final b
OutputStream stream = null;
final boolean fileExistedAlready = file.exists();
try {
- stream = Files.newOutputStream(file.toPath(), append ?
- new OpenOption[] {StandardOpenOption.APPEND} :
- new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
+ stream = FileUtils.newOutputStream(file, append);
if (encoding == null || encoding instanceof Charset) {
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
}

Refactor internals into new methods:

- Add FileUtils.newOutputStream(File, boolean).
- Add PathUtils.newOutputStream(Path, boolean).