You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2012/09/21 14:19:38 UTC
svn commit: r1388454 - in /maven/shared/trunk/maven-shared-utils/src:
main/java/org/apache/maven/shared/utils/io/FileUtils.java
test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
Author: struberg
Date: Fri Sep 21 12:19:37 2012
New Revision: 1388454
URL: http://svn.apache.org/viewvc?rev=1388454&view=rev
Log:
MSHARED-243 add file write and read for String arrays.
Modified:
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java?rev=1388454&r1=1388453&r2=1388454&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java Fri Sep 21 12:19:37 2012
@@ -89,6 +89,11 @@ import java.util.Random;
*/
public class FileUtils
{
+ private FileUtils()
+ {
+ // This is a utility class. We add a private ct to prevent initialisation
+ }
+
/**
* The number of bytes in a kilobyte.
*/
@@ -366,6 +371,20 @@ public class FileUtils
}
/**
+ * @param file the file path
+ * @return the file content lines as String[] using the systems default encoding.
+ * An empty List if the file didn't exist.
+ * @throws IOException
+ */
+ public static String[] fileReadArray( File file )
+ throws IOException
+ {
+ List<String> files = loadFile( file );
+
+ return files.toArray( new String[ files.size() ] );
+ }
+
+ /**
* Appends data to a file. The file will be created if it does not exist.
* Note: the data is written with platform encoding
*
@@ -487,6 +506,63 @@ public class FileUtils
}
/**
+ * Writes String array data to a file in the systems default encoding.
+ * The file will be created if it does not exist.
+ *
+ * @param file The path of the file to write.
+ * @param data The content to write to the file.
+ * @throws IOException if any
+ *
+ * @since 1.0
+ */
+ public static void fileWriteArray( File file, String[] data )
+ throws IOException
+ {
+ fileWriteArray( file, null, data );
+ }
+
+ /**
+ * Writes String array data to a file. The file will be created if it does not exist.
+ *
+ * @param file The path of the file to write.
+ * @param encoding The encoding of the file.
+ * @param data The content to write to the file.
+ * @throws IOException if any
+ *
+ * @since 1.0
+ */
+ public static void fileWriteArray( File file, String encoding, String[] data )
+ throws IOException
+ {
+ Writer writer = null;
+ try
+ {
+ OutputStream out = new FileOutputStream( file );
+ if ( encoding != null )
+ {
+ writer = new OutputStreamWriter( out, encoding );
+ }
+ else
+ {
+ writer = new OutputStreamWriter( out );
+ }
+
+ for ( int i = 0; data != null && i < data.length; i++ )
+ {
+ writer.write( data[ i ] );
+ if ( i < data.length )
+ {
+ writer.write( "\n" );
+ }
+ }
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
+ }
+
+ /**
* Deletes a file.
*
* @param fileName The path of the file to delete.
Modified: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java?rev=1388454&r1=1388453&r2=1388454&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java Fri Sep 21 12:19:37 2012
@@ -1082,6 +1082,29 @@ public class FileUtilsTest
}
@Test
+ public void writeStringArrayToFile()
+ throws Exception
+ {
+ File file = new File( tempFolder.getRoot(), "writeArray.txt" );
+ FileUtils.fileWriteArray( file, new String[]{"line1", "line2", "line3"} );
+
+ byte[] text = "line1\nline2\nline3".getBytes( "UTF8" );
+ assertEqualContent( text, file );
+ }
+
+ @Test
+ public void writeStringArrayToFileWithEncoding()
+ throws Exception
+ {
+ File file = new File( tempFolder.getRoot(), "writeArray.txt" );
+ FileUtils.fileWriteArray( file, "UTF8", new String[]{"line1", "line2", "line3"} );
+
+ byte[] text = "line1\nline2\nline3".getBytes( "UTF8" );
+ assertEqualContent( text, file );
+ }
+
+
+ @Test
public void writeWithEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
throws Exception
{