Skip to content

Commit b31de97

Browse files
author
Stephen Colebourne
committed
FileUtils.read/write byte array methods
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140588 13f79535-47bb-0310-9956-ffa450edef68
1 parent 07b4154 commit b31de97

2 files changed

Lines changed: 91 additions & 15 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
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
*/
6969
public 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.

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
import java.io.OutputStream;
2323
import java.net.URL;
2424

25-
import org.apache.commons.io.testtools.FileBasedTestCase;
26-
2725
import junit.framework.Test;
2826
import junit.framework.TestSuite;
2927
import junit.textui.TestRunner;
3028

29+
import org.apache.commons.io.testtools.FileBasedTestCase;
30+
3131
/**
3232
* This is used to test FileUtils for correctness.
3333
*
3434
* @author Peter Donald
3535
* @author Matthew Hawthorne
36-
* @version $Id: FileUtilsTestCase.java,v 1.20 2004/07/03 11:24:49 jeremias Exp $
36+
* @version $Id: FileUtilsTestCase.java,v 1.21 2004/07/15 08:21:14 scolebourne Exp $
3737
* @see FileUtils
3838
*/
3939
public class FileUtilsTestCase extends FileBasedTestCase {
@@ -467,4 +467,44 @@ public void testTouch() throws IOException {
467467
assertFalse("FileUtils.touch() changed lastModified.", 0 == file.lastModified()) ;
468468
}
469469

470+
public void testReadFileToString() throws Exception {
471+
File file = new File(getTestDirectory(), "read.obj");
472+
FileOutputStream out = new FileOutputStream(file);
473+
byte[] text = "Hello /u1234".getBytes("UTF8");
474+
out.write(text);
475+
out.close();
476+
477+
String data = FileUtils.readFileToString(file, "UTF8");
478+
assertEquals("Hello /u1234", data);
479+
}
480+
481+
public void testReadFileToByteArray() throws Exception {
482+
File file = new File(getTestDirectory(), "read.txt");
483+
FileOutputStream out = new FileOutputStream(file);
484+
out.write(11);
485+
out.write(21);
486+
out.write(31);
487+
out.close();
488+
489+
byte[] data = FileUtils.readFileToByteArray(file);
490+
assertEquals(3, data.length);
491+
assertEquals(11, data[0]);
492+
assertEquals(21, data[1]);
493+
assertEquals(31, data[2]);
494+
}
495+
496+
public void testWriteStringToFile() throws Exception {
497+
File file = new File(getTestDirectory(), "write.txt");
498+
FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
499+
byte[] text = "Hello /u1234".getBytes("UTF8");
500+
assertEqualContent(text, file);
501+
}
502+
503+
public void testWriteByteArrayToFile() throws Exception {
504+
File file = new File(getTestDirectory(), "write.obj");
505+
byte[] data = new byte[] {11, 21, 31};
506+
FileUtils.writeByteArrayToFile(file, data);
507+
assertEqualContent(data, file);
508+
}
509+
470510
}

0 commit comments

Comments
 (0)