Changeset 1451 for trunk/java-utils/src


Ignore:
Timestamp:
03/31/14 14:28:53 (10 years ago)
Author:
sherbold
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.