6464 * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
6565 * @author Matthew Hawthorne
6666 * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
67- * @version $Id: FileUtils.java,v 1.33 2004/07/03 11:20:45 jeremias Exp $
67+ * @version $Id: FileUtils.java,v 1.34 2004/07/15 08:21:14 scolebourne Exp $
6868 */
6969public class FileUtils {
7070
@@ -121,7 +121,7 @@ public static String byteCountToDisplaySize(long size) {
121121 * @throws IOException If an I/O problem occurs
122122 */
123123 public static void touch (File file ) throws IOException {
124- OutputStream out = new java . io . FileOutputStream (file , true );
124+ OutputStream out = new FileOutputStream (file , true );
125125 IOUtils .closeQuietly (out );
126126 file .setLastModified (System .currentTimeMillis ());
127127 }
@@ -271,8 +271,8 @@ public static boolean contentEquals(File file1, File file2)
271271 InputStream input1 = null ;
272272 InputStream input2 = null ;
273273 try {
274- input1 = new java . io . FileInputStream (file1 );
275- input2 = new java . io . FileInputStream (file2 );
274+ input1 = new FileInputStream (file1 );
275+ input2 = new FileInputStream (file2 );
276276 return IOUtils .contentEquals (input1 , input2 );
277277
278278 } finally {
@@ -574,16 +574,15 @@ public static boolean waitFor(File file, int seconds) {
574574 * in inconsistent results.
575575 * </p>
576576 *
577- * @param file the file to read.
578- * @param encoding the encoding to use
577+ * @param file the file to read
578+ * @param encoding the encoding to use
579579 * @return The file contents or null if read failed.
580580 * @throws IOException in case of an I/O error
581- * @throws UnsupportedEncodingException if the encoding is not supported
582- * by the VM
581+ * @throws UnsupportedEncodingException if the encoding is not supported by the VM
583582 */
584583 public static String readFileToString (
585584 File file , String encoding ) throws IOException {
586- InputStream in = new java . io . FileInputStream (file );
585+ InputStream in = new FileInputStream (file );
587586 try {
588587 return IOUtils .toString (in , encoding );
589588 } finally {
@@ -593,10 +592,28 @@ public static String readFileToString(
593592
594593 /**
595594 * <p>
596- * Writes data to a file. The file will be created if it does not exist .
595+ * Reads the contents of a file into a byte array .
597596 * </p>
597+ *
598+ * @param file the file to read
599+ * @return The file contents or null if read failed.
600+ * @throws IOException in case of an I/O error
601+ */
602+ public static byte [] readFileToByteArray (File file ) throws IOException {
603+ InputStream in = new FileInputStream (file );
604+ try {
605+ return IOUtils .toByteArray (in );
606+ } finally {
607+ IOUtils .closeQuietly (in );
608+ }
609+ }
610+
611+ /**
598612 * <p>
599- * There is no readFileToString method without encoding parameter because
613+ * Writes a String to a file creating the file if it does not exist.
614+ * </p>
615+ * <p>
616+ * There is no writeStringToFile method without encoding parameter because
600617 * the default encoding can differ between platforms and therefore results
601618 * in inconsistent results.
602619 * </p>
@@ -610,14 +627,33 @@ public static String readFileToString(
610627 */
611628 public static void writeStringToFile (File file ,
612629 String data , String encoding ) throws IOException {
613- OutputStream out = new java . io . FileOutputStream (file );
630+ OutputStream out = new FileOutputStream (file );
614631 try {
615632 out .write (data .getBytes (encoding ));
616633 } finally {
617634 IOUtils .closeQuietly (out );
618635 }
619636 }
620637
638+ /**
639+ * <p>
640+ * Writes a byte array to a file creating the file if it does not exist.
641+ * </p>
642+ *
643+ * @param file the file to write to
644+ * @param data the content to write to the file
645+ * @throws IOException in case of an I/O error
646+ */
647+ public static void writeByteArrayToFile (
648+ File file , byte [] data ) throws IOException {
649+ OutputStream out = new FileOutputStream (file );
650+ try {
651+ out .write (data );
652+ } finally {
653+ IOUtils .closeQuietly (out );
654+ }
655+ }
656+
621657 /**
622658 * <p>
623659 * Delete a file. If file is a directory, delete it and all sub-directories.
0 commit comments