Changeset 1451


Ignore:
Timestamp:
03/31/14 14:28:53 (10 years ago)
Author:
sherbold
Message:
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  
    1515package de.ugoe.cs.util; 
    1616 
     17import java.io.File; 
     18 
     19import junitx.framework.FileAssert; 
     20 
    1721import org.junit.*; 
     22 
    1823import static org.junit.Assert.*; 
    1924 
     
    7277                FileTools.getLinesFromFile(filenameNotExists, false); 
    7378        } 
     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        } 
    74140 
    75141        public static void main(String[] args) { 
  • trunk/java-utils/src/main/java/de/ugoe/cs/util/FileTools.java

    r1243 r1451  
    1515package de.ugoe.cs.util; 
    1616 
     17import java.io.BufferedWriter; 
    1718import java.io.File; 
    1819import java.io.FileInputStream; 
    1920import java.io.FileNotFoundException; 
    2021import java.io.FileReader; 
     22import java.io.FileWriter; 
    2123import java.io.IOException; 
    2224import java.io.InputStreamReader; 
     
    99101        return (new String(buffer)).split(splitString); 
    100102    } 
    101      
     103 
    102104    /** 
    103105     * <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. 
    106146     * </p> 
    107147     *  
     
    113153        File prefixFile = new File(prefix); 
    114154        File parentDir = prefixFile.getParentFile(); 
    115          
     155 
    116156        if (parentDir == null) { 
    117157            // the prefix does not denote a path or denotes one of the root directories. 
     
    119159            return prefix; 
    120160        } 
    121          
     161 
    122162        String[] completions = null; 
    123          
     163 
    124164        if (parentDir.exists()) { 
    125165            completions = parentDir.list(); 
    126166        } 
    127          
     167 
    128168        if (completions == null) { 
    129169            completions = new String[0]; 
    130170        } 
    131          
     171 
    132172        String completedPrefix; 
    133          
     173 
    134174        completedPrefix = StringTools.autocomplete(prefixFile.getName(), completions); 
    135          
     175 
    136176        File completedFile = new File(parentDir, completedPrefix); 
    137          
     177 
    138178        if (completedFile.exists() && completedFile.isDirectory()) { 
    139179            return completedFile.getAbsolutePath() + File.separator; 
Note: See TracChangeset for help on using the changeset viewer.