Changeset 1451
- Timestamp:
- 03/31/14 14:28:53 (11 years ago)
- Location:
- trunk
- Files:
-
- 4 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/java-utils-test/src/test/java/de/ugoe/cs/util/FileToolsTest.java
r927 r1451 15 15 package de.ugoe.cs.util; 16 16 17 import java.io.File; 18 19 import junitx.framework.FileAssert; 20 17 21 import org.junit.*; 22 18 23 import static org.junit.Assert.*; 19 24 … … 72 77 FileTools.getLinesFromFile(filenameNotExists, false); 73 78 } 79 80 @Test 81 public void testWriteArrayToFile_1() throws Exception { 82 String[] array = new String[]{"foo", "bar"}; 83 String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_01.txt"; 84 String separator = ""; 85 boolean lineBreak = false; 86 87 // cleanup old result 88 new File(filename).delete(); 89 90 FileTools.writeArrayToFile(array, filename, separator, lineBreak); 91 92 FileAssert.assertEquals(new File("testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_01.txt"), new File(filename)); 93 94 // cleanup after successful test 95 new File(filename).delete(); 96 } 97 98 @Test 99 public void testWriteArrayToFile_2() throws Exception { 100 String[] array = new String[]{"foo", "bar"}; 101 String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_02.txt"; 102 String separator = " - "; 103 boolean lineBreak = false; 104 105 // cleanup old result 106 new File(filename).delete(); 107 108 FileTools.writeArrayToFile(array, filename, separator, lineBreak); 109 110 FileAssert.assertEquals(new File("testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_02.txt"), new File(filename)); 111 112 // cleanup after successful test 113 new File(filename).delete(); 114 } 115 116 @Test 117 public void testWriteArrayToFile_3() throws Exception { 118 String[] array = new String[]{"foo", "bar"}; 119 String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_03.txt"; 120 String separator = ""; 121 boolean lineBreak = true; 122 123 // cleanup old result 124 new File(filename).delete(); 125 126 FileTools.writeArrayToFile(array, filename, separator, lineBreak); 127 128 String expectedFile; 129 if( System.getProperty("line.separator").equals("\r\n") ) { 130 expectedFile = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_03_cr.txt"; 131 } else { 132 expectedFile = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_03_nocr.txt"; 133 } 134 135 FileAssert.assertEquals(new File(expectedFile), new File(filename)); 136 137 // cleanup after successful test 138 new File(filename).delete(); 139 } 74 140 75 141 public static void main(String[] args) { -
trunk/java-utils/src/main/java/de/ugoe/cs/util/FileTools.java
r1243 r1451 15 15 package de.ugoe.cs.util; 16 16 17 import java.io.BufferedWriter; 17 18 import java.io.File; 18 19 import java.io.FileInputStream; 19 20 import java.io.FileNotFoundException; 20 21 import java.io.FileReader; 22 import java.io.FileWriter; 21 23 import java.io.IOException; 22 24 import java.io.InputStreamReader; … … 99 101 return (new String(buffer)).split(splitString); 100 102 } 101 103 102 104 /** 103 105 * <p> 104 * Autocompletes a give path. The path must be absolute. Otherwise, autocompletion 105 * is not possible. 106 * Writes an array to a file using the toString function of its object. 107 * </p> 108 * 109 * @param array 110 * array that is written 111 * @param filename 112 * name of the output file 113 * @param separator 114 * separator string that is put between the array objects 115 * @param lineBreak 116 * if true, a new line is added after each array element and its separator 117 * @throws IOException 118 * see {@link BufferedWriter#write(String)}, {@link BufferedWriter#newLine()}, 119 * {@link BufferedWriter#flush()}, {@link BufferedWriter#close()}, 120 * {@link FileWriter#FileWriter(String)} 121 */ 122 public static <T> void writeArrayToFile(T[] array, 123 String filename, 124 String separator, 125 boolean lineBreak) throws IOException 126 { 127 BufferedWriter outputWriter = null; 128 outputWriter = new BufferedWriter(new FileWriter(filename)); 129 for (int i = 0; i < array.length; i++) { 130 outputWriter.write(array[i].toString()); 131 if (i < array.length - 1) { 132 outputWriter.write(separator); 133 if (lineBreak) { 134 outputWriter.newLine(); 135 } 136 } 137 } 138 outputWriter.flush(); 139 outputWriter.close(); 140 } 141 142 /** 143 * <p> 144 * Autocompletes a given path. The path must be absolute. Otherwise, autocompletion is not 145 * possible. 106 146 * </p> 107 147 * … … 113 153 File prefixFile = new File(prefix); 114 154 File parentDir = prefixFile.getParentFile(); 115 155 116 156 if (parentDir == null) { 117 157 // the prefix does not denote a path or denotes one of the root directories. … … 119 159 return prefix; 120 160 } 121 161 122 162 String[] completions = null; 123 163 124 164 if (parentDir.exists()) { 125 165 completions = parentDir.list(); 126 166 } 127 167 128 168 if (completions == null) { 129 169 completions = new String[0]; 130 170 } 131 171 132 172 String completedPrefix; 133 173 134 174 completedPrefix = StringTools.autocomplete(prefixFile.getName(), completions); 135 175 136 176 File completedFile = new File(parentDir, completedPrefix); 137 177 138 178 if (completedFile.exists() && completedFile.isDirectory()) { 139 179 return completedFile.getAbsolutePath() + File.separator;
Note: See TracChangeset
for help on using the changeset viewer.