You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/09/28 22:44:34 UTC

svn commit: r292293 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileUtils.java src/test/org/apache/commons/io/FileUtilsTestCase.java

Author: scolebourne
Date: Wed Sep 28 13:44:27 2005
New Revision: 292293

URL: http://svn.apache.org/viewcvs?rev=292293&view=rev
Log:
Add FileUtils.writeLines

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=292293&r1=292292&r2=292293&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Wed Sep 28 13:44:27 2005
@@ -107,7 +107,7 @@
 - IOUtils - writeLines(Collection,lineEnding,OutputStream)  [36214]
           - writeLines(Collection,lineEnding,OutputStream,encoding)
           - writeLines(Collection,lineEnding,Writer)
-    Writes a collection to a file line by line
+    Writes a collection to a stream/writer line by line
 
 - IOUtils - write(...)
     Write data to a stream/writer (moved from CopyUtils with better null handling)
@@ -132,6 +132,10 @@
 
 - FileUtils - readLines(File,encoding)  [36214]
     Reads a file line by line into a List of Strings
+
+- FileUtils - writeLines(File,encoding,List)
+              writeLines(File,encoding,List,lineEnding)
+    Writes a collection to a file line by line
 
 - FileUtils - EMPTY_FILE_ARRAY
     Constant for an empty array of File objects

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=292293&r1=292292&r2=292293&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Wed Sep 28 13:44:27 2005
@@ -812,7 +812,7 @@
      * @return the list of Strings representing each line in the file
      * @throws IOException in case of an I/O error
      * @throws UnsupportedEncodingException if the encoding is not supported by the VM
-     * @since 1.1
+     * @since Commons IO 1.1
      */
     public static List readLines(File file, String encoding) throws IOException {
         InputStream in = new FileInputStream(file);
@@ -834,12 +834,11 @@
      * in inconsistent results.
      * </p>
      *
-     * @param file the file to write.
-     * @param data The content to write to the file.
-     * @param encoding encoding to use
+     * @param file  the file to write
+     * @param data  the content to write to the file
+     * @param encoding  encoding to use
      * @throws IOException in case of an I/O error
-     * @throws UnsupportedEncodingException if the encoding is not supported
-     *   by the VM
+     * @throws UnsupportedEncodingException if the encoding is not supported by the VM
      */
     public static void writeStringToFile(File file,
             String data, String encoding) throws IOException {
@@ -871,6 +870,61 @@
         }
     }
 
+    /**
+     * <p>
+     * Writes the <code>toString()</code> value of each item in a collection to
+     * the specified <code>File</code> line by line.
+     * The specified character encoding and the default line ending will be used.
+     * </p>
+     * <p>
+     * There is no writeLines method without encoding parameter because
+     * the default encoding can differ between platforms and therefore results
+     * in inconsistent results.
+     * </p>
+     *
+     * @param file  the file to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @param lines  the lines to write, null entries produce blank lines
+     * @return the list of Strings representing each line in the file
+     * @throws IOException in case of an I/O error
+     * @throws UnsupportedEncodingException if the encoding is not supported by the VM
+     * @since Commons IO 1.1
+     */
+    public static void writeLines(File file, String encoding, Collection lines) throws IOException {
+        writeLines(file, encoding, lines, null);
+    }
+
+    /**
+     * <p>
+     * Writes the <code>toString()</code> value of each item in a collection to
+     * the specified <code>File</code> line by line.
+     * The specified character encoding and the line ending will be used.
+     * </p>
+     * <p>
+     * There is no writeLines method without encoding parameter because
+     * the default encoding can differ between platforms and therefore results
+     * in inconsistent results.
+     * </p>
+     *
+     * @param file  the file to write to
+     * @param encoding  the encoding to use, null means platform default
+     * @param lines  the lines to write, null entries produce blank lines
+     * @param lineEnding  the line separator to use, null is system default
+     * @return the list of Strings representing each line in the file
+     * @throws IOException in case of an I/O error
+     * @throws UnsupportedEncodingException if the encoding is not supported by the VM
+     * @since Commons IO 1.1
+     */
+    public static void writeLines(File file, String encoding, Collection lines, String lineEnding) throws IOException {
+        OutputStream out = new FileOutputStream(file);
+        try {
+            IOUtils.writeLines(lines, lineEnding, out, encoding);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    //-----------------------------------------------------------------------
     /**
      * <p>
      * Delete a file. If file is a directory, delete it and all sub-directories.

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=292293&r1=292292&r2=292293&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Wed Sep 28 13:44:27 2005
@@ -20,6 +20,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.GregorianCalendar;
@@ -29,7 +31,9 @@
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
+import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.commons.io.testtools.FileBasedTestCase;
+import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
 
 /**
  * This is used to test FileUtils for correctness.
@@ -667,7 +671,7 @@
         assertEquals(31, data[2]);
     }
 
-    public void testReadLines_Reader() throws Exception {
+    public void testReadLines() throws Exception {
         File file = newFile("lines.txt");
         try {
             String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
@@ -680,18 +684,75 @@
         }
     }
 
-    public void testWriteStringToFile() throws Exception {
+    public void testWriteStringToFile1() throws Exception {
         File file = new File(getTestDirectory(), "write.txt");
         FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
         byte[] text = "Hello /u1234".getBytes("UTF8");
         assertEqualContent(text, file);
     }
 
+    public void testWriteStringToFile2() throws Exception {
+        File file = new File(getTestDirectory(), "write.txt");
+        FileUtils.writeStringToFile(file, "Hello /u1234", null);
+        byte[] text = "Hello /u1234".getBytes();
+        assertEqualContent(text, file);
+    }
+
     public void testWriteByteArrayToFile() throws Exception {
         File file = new File(getTestDirectory(), "write.obj");
         byte[] data = new byte[] {11, 21, 31};
         FileUtils.writeByteArrayToFile(file, data);
         assertEqualContent(data, file);
+    }
+
+    public void testWriteLines_4arg() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        File file = newFile("lines.txt");
+        FileUtils.writeLines(file, "US-ASCII", list, "*");
+        
+        String expected = "hello*world**this is**some text*";
+        String actual = FileUtils.readFileToString(file, "US-ASCII");
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_4arg_Writer_nullData() throws Exception {
+        File file = newFile("lines.txt");
+        FileUtils.writeLines(file, "US-ASCII", (List) null, "*");
+        
+        assertEquals("Sizes differ", 0, file.length());
+    }
+
+    public void testWriteLines_4arg_nullSeparator() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        File file = newFile("lines.txt");
+        FileUtils.writeLines(file, "US-ASCII", list, null);
+        
+        String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
+            IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
+            IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
+        String actual = FileUtils.readFileToString(file, "US-ASCII");
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteLines_3arg_nullSeparator() throws Exception {
+        Object[] data = new Object[] {
+            "hello", new StringBuffer("world"), "", "this is", null, "some text"};
+        List list = Arrays.asList(data);
+        
+        File file = newFile("lines.txt");
+        FileUtils.writeLines(file, "US-ASCII", list);
+        
+        String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
+            IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
+            IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
+        String actual = FileUtils.readFileToString(file, "US-ASCII");
+        assertEquals(expected, actual);
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org