@@ -116,6 +116,7 @@ public FileUtils() {
116116 * The parent directory will be created if it does not exist.
117117 * The file will be created if it does not exist.
118118 * An exception is thrown if the file object exists but is a directory.
119+ * An exception is thrown if the file exists but cannot be written to.
119120 * An exception is thrown if the parent directory cannot be created.
120121 *
121122 * @param file the file to create, not null
@@ -127,6 +128,9 @@ public static FileOutputStream openOutputStream(File file) throws IOException {
127128 if (file .isDirectory ()) {
128129 throw new IOException ("File '" + file + "' exists but is a directory" );
129130 }
131+ if (file .canWrite () == false ) {
132+ throw new IOException ("File '" + file + "' cannot be written to" );
133+ }
130134 } else {
131135 File parent = file .getParentFile ();
132136 if (parent .exists () == false ) {
@@ -168,14 +172,15 @@ public static String byteCountToDisplaySize(long size) {
168172 * closed without modifying it, but updating the file date and time.
169173 * <p>
170174 * NOTE: As from v1.3, this method throws an IOException if the last
171- * modified date of the file cannot be set
175+ * modified date of the file cannot be set. Also, as from v1.3 this method
176+ * creates parent directories if they do not exist.
172177 *
173178 * @param file the File to touch
174179 * @throws IOException If an I/O problem occurs
175180 */
176181 public static void touch (File file ) throws IOException {
177182 if (!file .exists ()) {
178- OutputStream out = new FileOutputStream (file );
183+ OutputStream out = openOutputStream (file );
179184 IOUtils .closeQuietly (out );
180185 }
181186 boolean success = file .setLastModified (System .currentTimeMillis ());
@@ -790,34 +795,19 @@ private static void doCopyDirectory(File srcDir, File destDir, boolean preserveF
790795 * will be created if they don't already exist. <code>destination</code>
791796 * will be overwritten if it already exists.
792797 *
793- * @param source A <code>URL</code> to copy bytes from.
794- * @param destination A non-directory <code>File</code> to write bytes to
795- * (possibly overwriting).
796- *
797- * @throws IOException if
798- * <ul>
799- * <li><code>source</code> URL cannot be opened</li>
800- * <li><code>destination</code> cannot be written to</li>
801- * <li>an IO error occurs during copying</li>
802- * </ul>
798+ * @param source the <code>URL</code> to copy bytes from, not null
799+ * @param destination the non-directory <code>File</code> to write bytes to
800+ * (possibly overwriting), not null
801+ * @throws IOException if <code>source</code> URL cannot be opened
802+ * @throws IOException if <code>destination</code> is a directory
803+ * @throws IOException if <code>destination</code> cannot be written
804+ * @throws IOException if <code>destination</code> needs creating but can't be
805+ * @throws IOException if an IO error occurs during copying
803806 */
804807 public static void copyURLToFile (URL source , File destination ) throws IOException {
805- //does destination directory exist ?
806- if (destination .getParentFile () != null
807- && !destination .getParentFile ().exists ()) {
808- destination .getParentFile ().mkdirs ();
809- }
810-
811- //make sure we can write to destination
812- if (destination .exists () && !destination .canWrite ()) {
813- String message =
814- "Unable to open file " + destination + " for writing." ;
815- throw new IOException (message );
816- }
817-
818808 InputStream input = source .openStream ();
819809 try {
820- FileOutputStream output = new FileOutputStream (destination );
810+ FileOutputStream output = openOutputStream (destination );
821811 try {
822812 IOUtils .copy (input , output );
823813 } finally {
@@ -835,8 +825,7 @@ public static void copyURLToFile(URL source, File destination) throws IOExceptio
835825 * @param directory directory to delete
836826 * @throws IOException in case deletion is unsuccessful
837827 */
838- public static void deleteDirectory (File directory )
839- throws IOException {
828+ public static void deleteDirectory (File directory ) throws IOException {
840829 if (!directory .exists ()) {
841830 return ;
842831 }
@@ -1045,16 +1034,18 @@ public static LineIterator lineIterator(File file, String encoding) throws IOExc
10451034 * There is no writeStringToFile method without encoding parameter because
10461035 * the default encoding can differ between platforms and will have
10471036 * inconsistent results.
1037+ * <p>
1038+ * NOTE: As from v1.3, the parent directories of the file will be created
1039+ * if they do not exist.
10481040 *
10491041 * @param file the file to write
10501042 * @param data the content to write to the file
10511043 * @param encoding the encoding to use, null means platform default
10521044 * @throws IOException in case of an I/O error
10531045 * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
10541046 */
1055- public static void writeStringToFile (File file ,
1056- String data , String encoding ) throws IOException {
1057- OutputStream out = new FileOutputStream (file );
1047+ public static void writeStringToFile (File file , String data , String encoding ) throws IOException {
1048+ OutputStream out = openOutputStream (file );
10581049 try {
10591050 IOUtils .write (data , out , encoding );
10601051 } finally {
@@ -1064,15 +1055,17 @@ public static void writeStringToFile(File file,
10641055
10651056 /**
10661057 * Writes a byte array to a file creating the file if it does not exist.
1058+ * <p>
1059+ * NOTE: As from v1.3, the parent directories of the file will be created
1060+ * if they do not exist.
10671061 *
10681062 * @param file the file to write to
10691063 * @param data the content to write to the file
10701064 * @throws IOException in case of an I/O error
10711065 * @since Commons IO 1.1
10721066 */
1073- public static void writeByteArrayToFile (
1074- File file , byte [] data ) throws IOException {
1075- OutputStream out = new FileOutputStream (file );
1067+ public static void writeByteArrayToFile (File file , byte [] data ) throws IOException {
1068+ OutputStream out = openOutputStream (file );
10761069 try {
10771070 out .write (data );
10781071 } finally {
@@ -1088,6 +1081,9 @@ public static void writeByteArrayToFile(
10881081 * There is no writeLines method without encoding parameter because
10891082 * the default encoding can differ between platforms and will have
10901083 * inconsistent results.
1084+ * <p>
1085+ * NOTE: As from v1.3, the parent directories of the file will be created
1086+ * if they do not exist.
10911087 *
10921088 * @param file the file to write to
10931089 * @param encoding the encoding to use, null means platform default
@@ -1108,6 +1104,9 @@ public static void writeLines(File file, String encoding, Collection lines) thro
11081104 * There is no writeLines method without encoding parameter because
11091105 * the default encoding can differ between platforms and will have
11101106 * inconsistent results.
1107+ * <p>
1108+ * NOTE: As from v1.3, the parent directories of the file will be created
1109+ * if they do not exist.
11111110 *
11121111 * @param file the file to write to
11131112 * @param encoding the encoding to use, null means platform default
@@ -1118,7 +1117,7 @@ public static void writeLines(File file, String encoding, Collection lines) thro
11181117 * @since Commons IO 1.1
11191118 */
11201119 public static void writeLines (File file , String encoding , Collection lines , String lineEnding ) throws IOException {
1121- OutputStream out = new FileOutputStream (file );
1120+ OutputStream out = openOutputStream (file );
11221121 try {
11231122 IOUtils .writeLines (lines , lineEnding , out , encoding );
11241123 } finally {
0 commit comments