@@ -220,6 +220,32 @@ public static FileInputStream openInputStream(File file) throws IOException {
220220 * @since Commons IO 1.3
221221 */
222222 public static FileOutputStream openOutputStream (File file ) throws IOException {
223+ return openOutputStream (file , false );
224+ }
225+
226+ /**
227+ * Opens a {@link FileOutputStream} for the specified file, checking and
228+ * creating the parent directory if it does not exist.
229+ * <p>
230+ * At the end of the method either the stream will be successfully opened,
231+ * or an exception will have been thrown.
232+ * <p>
233+ * The parent directory will be created if it does not exist.
234+ * The file will be created if it does not exist.
235+ * An exception is thrown if the file object exists but is a directory.
236+ * An exception is thrown if the file exists but cannot be written to.
237+ * An exception is thrown if the parent directory cannot be created.
238+ *
239+ * @param file the file to open for output, must not be <code>null</code>
240+ * @param append if <code>true</code>, then bytes will be added to the
241+ * end of the file rather than overwriting
242+ * @return a new {@link FileOutputStream} for the specified file
243+ * @throws IOException if the file object is a directory
244+ * @throws IOException if the file cannot be written to
245+ * @throws IOException if a parent directory needs creating but that fails
246+ * @since Commons IO 2.1
247+ */
248+ public static FileOutputStream openOutputStream (File file , boolean append ) throws IOException {
223249 if (file .exists ()) {
224250 if (file .isDirectory ()) {
225251 throw new IOException ("File '" + file + "' exists but is a directory" );
@@ -235,7 +261,7 @@ public static FileOutputStream openOutputStream(File file) throws IOException {
235261 }
236262 }
237263 }
238- return new FileOutputStream (file );
264+ return new FileOutputStream (file , append );
239265 }
240266
241267 //-----------------------------------------------------------------------
@@ -1469,9 +1495,25 @@ public static LineIterator lineIterator(File file) throws IOException {
14691495 * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
14701496 */
14711497 public static void writeStringToFile (File file , String data , String encoding ) throws IOException {
1498+ writeStringToFile (file , data , encoding , false );
1499+ }
1500+
1501+ /**
1502+ * Writes a String to a file creating the file if it does not exist.
1503+ *
1504+ * @param file the file to write
1505+ * @param data the content to write to the file
1506+ * @param encoding the encoding to use, <code>null</code> means platform default
1507+ * @param append if <code>true</code>, then the String will be added to the
1508+ * end of the file rather than overwriting
1509+ * @throws IOException in case of an I/O error
1510+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1511+ * @since Commons IO 2.1
1512+ */
1513+ public static void writeStringToFile (File file , String data , String encoding , boolean append ) throws IOException {
14721514 OutputStream out = null ;
14731515 try {
1474- out = openOutputStream (file );
1516+ out = openOutputStream (file , append );
14751517 IOUtils .write (data , out , encoding );
14761518 } finally {
14771519 IOUtils .closeQuietly (out );
@@ -1486,7 +1528,21 @@ public static void writeStringToFile(File file, String data, String encoding) th
14861528 * @throws IOException in case of an I/O error
14871529 */
14881530 public static void writeStringToFile (File file , String data ) throws IOException {
1489- writeStringToFile (file , data , null );
1531+ writeStringToFile (file , data , null , false );
1532+ }
1533+
1534+ /**
1535+ * Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
1536+ *
1537+ * @param file the file to write
1538+ * @param data the content to write to the file
1539+ * @param append if <code>true</code>, then the String will be added to the
1540+ * end of the file rather than overwriting
1541+ * @throws IOException in case of an I/O error
1542+ * @since Commons IO 2.1
1543+ */
1544+ public static void writeStringToFile (File file , String data , boolean append ) throws IOException {
1545+ writeStringToFile (file , data , null , append );
14901546 }
14911547
14921548 /**
@@ -1498,8 +1554,21 @@ public static void writeStringToFile(File file, String data) throws IOException
14981554 * @since Commons IO 2.0
14991555 */
15001556 public static void write (File file , CharSequence data ) throws IOException {
1501- String str = data == null ? null : data .toString ();
1502- writeStringToFile (file , str );
1557+ write (file , data , null , false );
1558+ }
1559+
1560+ /**
1561+ * Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
1562+ *
1563+ * @param file the file to write
1564+ * @param data the content to write to the file
1565+ * @param append if <code>true</code>, then the data will be added to the
1566+ * end of the file rather than overwriting
1567+ * @throws IOException in case of an I/O error
1568+ * @since Commons IO 2.1
1569+ */
1570+ public static void write (File file , CharSequence data , boolean append ) throws IOException {
1571+ write (file , data , null , append );
15031572 }
15041573
15051574 /**
@@ -1513,8 +1582,24 @@ public static void write(File file, CharSequence data) throws IOException {
15131582 * @since Commons IO 2.0
15141583 */
15151584 public static void write (File file , CharSequence data , String encoding ) throws IOException {
1585+ write (file , data , encoding , false );
1586+ }
1587+
1588+ /**
1589+ * Writes a CharSequence to a file creating the file if it does not exist.
1590+ *
1591+ * @param file the file to write
1592+ * @param data the content to write to the file
1593+ * @param encoding the encoding to use, <code>null</code> means platform default
1594+ * @param append if <code>true</code>, then the data will be added to the
1595+ * end of the file rather than overwriting
1596+ * @throws IOException in case of an I/O error
1597+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1598+ * @since IO 2.1
1599+ */
1600+ public static void write (File file , CharSequence data , String encoding , boolean append ) throws IOException {
15161601 String str = data == null ? null : data .toString ();
1517- writeStringToFile (file , str , encoding );
1602+ writeStringToFile (file , str , encoding , append );
15181603 }
15191604
15201605 /**
@@ -1529,9 +1614,23 @@ public static void write(File file, CharSequence data, String encoding) throws I
15291614 * @since Commons IO 1.1
15301615 */
15311616 public static void writeByteArrayToFile (File file , byte [] data ) throws IOException {
1617+ writeByteArrayToFile (file , data , false );
1618+ }
1619+
1620+ /**
1621+ * Writes a byte array to a file creating the file if it does not exist.
1622+ *
1623+ * @param file the file to write to
1624+ * @param data the content to write to the file
1625+ * @param append if <code>true</code>, then bytes will be added to the
1626+ * end of the file rather than overwriting
1627+ * @throws IOException in case of an I/O error
1628+ * @since IO 2.1
1629+ */
1630+ public static void writeByteArrayToFile (File file , byte [] data , boolean append ) throws IOException {
15321631 OutputStream out = null ;
15331632 try {
1534- out = openOutputStream (file );
1633+ out = openOutputStream (file , append );
15351634 out .write (data );
15361635 } finally {
15371636 IOUtils .closeQuietly (out );
@@ -1554,7 +1653,25 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti
15541653 * @since Commons IO 1.1
15551654 */
15561655 public static void writeLines (File file , String encoding , Collection <?> lines ) throws IOException {
1557- writeLines (file , encoding , lines , null );
1656+ writeLines (file , encoding , lines , null , false );
1657+ }
1658+
1659+ /**
1660+ * Writes the <code>toString()</code> value of each item in a collection to
1661+ * the specified <code>File</code> line by line, optionally appending.
1662+ * The specified character encoding and the default line ending will be used.
1663+ *
1664+ * @param file the file to write to
1665+ * @param encoding the encoding to use, <code>null</code> means platform default
1666+ * @param lines the lines to write, <code>null</code> entries produce blank lines
1667+ * @param append if <code>true</code>, then the lines will be added to the
1668+ * end of the file rather than overwriting
1669+ * @throws IOException in case of an I/O error
1670+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1671+ * @since Commons IO 2.1
1672+ */
1673+ public static void writeLines (File file , String encoding , Collection <?> lines , boolean append ) throws IOException {
1674+ writeLines (file , encoding , lines , null , append );
15581675 }
15591676
15601677 /**
@@ -1568,7 +1685,23 @@ public static void writeLines(File file, String encoding, Collection<?> lines) t
15681685 * @since Commons IO 1.3
15691686 */
15701687 public static void writeLines (File file , Collection <?> lines ) throws IOException {
1571- writeLines (file , null , lines , null );
1688+ writeLines (file , null , lines , null , false );
1689+ }
1690+
1691+ /**
1692+ * Writes the <code>toString()</code> value of each item in a collection to
1693+ * the specified <code>File</code> line by line.
1694+ * The default VM encoding and the default line ending will be used.
1695+ *
1696+ * @param file the file to write to
1697+ * @param lines the lines to write, <code>null</code> entries produce blank lines
1698+ * @param append if <code>true</code>, then the lines will be added to the
1699+ * end of the file rather than overwriting
1700+ * @throws IOException in case of an I/O error
1701+ * @since Commons IO 2.1
1702+ */
1703+ public static void writeLines (File file , Collection <?> lines , boolean append ) throws IOException {
1704+ writeLines (file , null , lines , null , append );
15721705 }
15731706
15741707 /**
@@ -1588,10 +1721,30 @@ public static void writeLines(File file, Collection<?> lines) throws IOException
15881721 * @since Commons IO 1.1
15891722 */
15901723 public static void writeLines (File file , String encoding , Collection <?> lines , String lineEnding )
1724+ throws IOException {
1725+ writeLines (file , encoding , lines , lineEnding , false );
1726+ }
1727+
1728+ /**
1729+ * Writes the <code>toString()</code> value of each item in a collection to
1730+ * the specified <code>File</code> line by line.
1731+ * The specified character encoding and the line ending will be used.
1732+ *
1733+ * @param file the file to write to
1734+ * @param encoding the encoding to use, <code>null</code> means platform default
1735+ * @param lines the lines to write, <code>null</code> entries produce blank lines
1736+ * @param lineEnding the line separator to use, <code>null</code> is system default
1737+ * @param append if <code>true</code>, then the lines will be added to the
1738+ * end of the file rather than overwriting
1739+ * @throws IOException in case of an I/O error
1740+ * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM
1741+ * @since Commons IO 2.1
1742+ */
1743+ public static void writeLines (File file , String encoding , Collection <?> lines , String lineEnding , boolean append )
15911744 throws IOException {
15921745 OutputStream out = null ;
15931746 try {
1594- out = openOutputStream (file );
1747+ out = openOutputStream (file , append );
15951748 IOUtils .writeLines (lines , lineEnding , out , encoding );
15961749 } finally {
15971750 IOUtils .closeQuietly (out );
@@ -1610,7 +1763,25 @@ public static void writeLines(File file, String encoding, Collection<?> lines, S
16101763 * @since Commons IO 1.3
16111764 */
16121765 public static void writeLines (File file , Collection <?> lines , String lineEnding ) throws IOException {
1613- writeLines (file , null , lines , lineEnding );
1766+ writeLines (file , null , lines , lineEnding , false );
1767+ }
1768+
1769+ /**
1770+ * Writes the <code>toString()</code> value of each item in a collection to
1771+ * the specified <code>File</code> line by line.
1772+ * The default VM encoding and the specified line ending will be used.
1773+ *
1774+ * @param file the file to write to
1775+ * @param lines the lines to write, <code>null</code> entries produce blank lines
1776+ * @param lineEnding the line separator to use, <code>null</code> is system default
1777+ * @param append if <code>true</code>, then the lines will be added to the
1778+ * end of the file rather than overwriting
1779+ * @throws IOException in case of an I/O error
1780+ * @since Commons IO 2.1
1781+ */
1782+ public static void writeLines (File file , Collection <?> lines , String lineEnding , boolean append )
1783+ throws IOException {
1784+ writeLines (file , null , lines , lineEnding , append );
16141785 }
16151786
16161787 //-----------------------------------------------------------------------
0 commit comments