Example usage for org.apache.commons.io FileUtils writeLines

List of usage examples for org.apache.commons.io FileUtils writeLines

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils writeLines.

Prototype

public static void writeLines(File file, Collection lines) throws IOException 

Source Link

Document

Writes the toString() value of each item in a collection to the specified File line by line.

Usage

From source file:ca.weblite.xmlvm.VtableHelper.java

/**
 * Fixes the vTable references for a .c (or .m) source file.
 * @param file//from   w  ww.j  av a 2  s.  co  m
 * @param cFilesDirectory
 * @throws IOException 
 */
public static void fixVtableReferences(Project project, File file, File cFilesDirectory) throws IOException {
    if (file.isDirectory()) {
        for (File child : file.listFiles()) {

            fixVtableReferences(project, child, cFilesDirectory);
        }
        return;
    }

    if (file.getName().endsWith(".m") || file.getName().endsWith(".c")) {

    } else {
        return;
    }

    List<String> lines = FileUtils.readLines(file);
    List<String> out = new ArrayList<String>();

    // Pattern to match vtable error comments.  group(1) will contain function name
    String pattern = "^//([a-zA-Z][a-zA-Z0-9_]+)\\[-1]$";
    Pattern regex = Pattern.compile(pattern);

    // Pattern to match class name on a line that has a missing vtable entry
    // group(1) will contain the c prefix for the class name
    String clsPattern = "\\(\\(([a-zA-Z][a-zA-Z0-9_]*)\\*\\) _r\\d+\\.o\\)->tib->vtable";
    Pattern clsRegex = Pattern.compile(clsPattern);

    String vtableLinePattern = "\\(\\*\\(.*\\)->tib->vtable\\[-1\\]\\)";
    Pattern vtableLineRegex = Pattern.compile(vtableLinePattern);

    boolean inErrorSection = false;
    String functionName = null;
    for (String line : lines) {
        if (line.startsWith("XMLVM_ERROR(\"Missing @vtable-index\"")) {
            inErrorSection = true;
            line = "//" + line;

        } else if (inErrorSection) {
            if (functionName == null) {
                Matcher m = regex.matcher(line.trim());
                if (m.find()) {
                    functionName = m.group(1);
                }

            } else {
                if (line.indexOf("->tib->vtable[-1]") > 0) {
                    //line = line.replaceAll("->tib->vtable\\[-1\\]", "->tib->vtable["+)

                    //String vtableIdxPattern = "XMLVM_VTABLE_IDX_"+functionName;
                    boolean foundVtableEntry = false;
                    // Need to find the class name because we need to check
                    // if the class has a vtable entry defined for this method.
                    Matcher m = clsRegex.matcher(line);
                    if (m.find()) {
                        String clsName = m.group(1);
                        String mangledMethodName = functionName.substring(clsName.length() + 1);
                        File clsHeaderFile = new File(cFilesDirectory, clsName + ".h");

                        if (clsHeaderFile.exists()) {
                            Path srcPaths = new Path(project);
                            srcPaths.add(new Path(project, file.getAbsolutePath()));
                            srcPaths.add(new Path(project, cFilesDirectory.getAbsolutePath()));
                            String resolvedFunction = resolveVirtualMethod(project, srcPaths, clsHeaderFile,
                                    mangledMethodName);
                            if (resolvedFunction.startsWith("XMLVM_VTABLE_IDX")) {
                                line = line.replaceAll("->tib->vtable\\[-1\\]",
                                        "->tib->vtable[" + resolvedFunction + "]");
                                foundVtableEntry = true;
                            } else {
                                line = line.replaceAll(vtableLinePattern, resolvedFunction);
                            }

                        } else {
                            throw new RuntimeException("No class header found for class " + clsName);
                        }
                    } else {
                        throw new RuntimeException(
                                "Found missing vtable entry but regex failed to find class name: " + line);
                    }

                    functionName = null;
                    inErrorSection = false;

                }
            }
        }
        out.add(line);
    }

    FileUtils.writeLines(file, out);

}

From source file:com.thesmartweb.swebrank.TFIDF.java

/**
 * Method to compute the TFIDF score/*from w ww  .  j  a  v a 2  s.c om*/
 * @param allDocs all the documents to analyze
 * @param topWords the amount of top words to get
 * @param directory the directory to save the output
 * @return a list with the top words
 */
public List<String> compute(String[] allDocs, int topWords, String directory) {
    try {
        List<List<String>> allwordsList = new ArrayList<>();
        int counterwords = 0;
        int negtfidf = 0;
        for (int i = 0; i < allDocs.length; i++) {
            List<String> allwordsList_single = new ArrayList<>();
            if (!(allDocs[i] == null)) {
                String stringtosplit = allDocs[i];
                if (!(stringtosplit == null) && (!(stringtosplit.equalsIgnoreCase("")))) {
                    stringtosplit = stringtosplit.replaceAll("[\\W&&[^\\s]]", "");
                    if (!(stringtosplit == null) && (!(stringtosplit.equalsIgnoreCase("")))) {
                        String[] tokenizedTerms = stringtosplit.split("\\W+");
                        for (int j = 0; j < tokenizedTerms.length; j++) {
                            if (!(tokenizedTerms[j] == null) && (!(tokenizedTerms[j].equalsIgnoreCase("")))) {
                                allwordsList_single.add(tokenizedTerms[j]);
                                counterwords++;
                            }
                        }
                    }
                }
            }
            allwordsList.add(i, allwordsList_single);
        }

        HashMap<String, Double> wordTFIDFscores = new HashMap<>();
        List<String> topwordsTFIDF;
        topwordsTFIDF = new ArrayList<>();
        List<String> wordsTFIDF = new ArrayList<>();
        List<Double> TFIDFscoreslist;
        List<Double> TFIDFscoreslistcopy = new ArrayList<>();
        TFIDFscoreslist = new ArrayList<>();
        for (int i = 0; i < allDocs.length; i++) {
            if (!(allDocs[i] == null)) {
                String stringtosplit = allDocs[i];
                if (!(stringtosplit == null) && (!(stringtosplit.equalsIgnoreCase("")))) {
                    stringtosplit = stringtosplit.replaceAll("[\\W&&[^\\s]]", "");
                    if (!(stringtosplit == null) && (!(stringtosplit.equalsIgnoreCase("")))) {
                        String[] tokenizedTerms = stringtosplit.split("\\W+");
                        for (int j = 0; j < tokenizedTerms.length; j++) {
                            if (!(tokenizedTerms[j] == null) && (!(tokenizedTerms[j].equalsIgnoreCase("")))) {
                                Double tfvalue = tfCalculator(allDocs[i], tokenizedTerms[j]);
                                Double idfvalue = idfCalculator(allwordsList, tokenizedTerms[j],
                                        allDocs.length);
                                Double tfidfvalue = tfvalue * idfvalue;
                                if (tfidfvalue < 0) {
                                    negtfidf++;
                                }
                                TFIDFscoreslist.add(tfvalue.doubleValue());
                                TFIDFscoreslistcopy.add(tfvalue.doubleValue());
                                wordsTFIDF.add(tokenizedTerms[j]);
                                if (wordTFIDFscores.get(tokenizedTerms[j]) == null
                                        || wordTFIDFscores.get(tokenizedTerms[j]).doubleValue() > tfidfvalue) {
                                    wordTFIDFscores.put(tokenizedTerms[j], tfidfvalue);
                                }
                            }
                        }
                    }
                }
            }
        }
        DataManipulation shmap = new DataManipulation();
        topwordsTFIDF = shmap.sortHashmap(wordTFIDFscores).subList(0, topWords);
        topWordsList = topwordsTFIDF;
        File file_words = new File(directory + "words.txt");
        FileUtils.writeLines(file_words, topWordsList);
        return topWordsList;
    } catch (IOException ex) {
        Logger.getLogger(TFIDF.class.getName()).log(Level.SEVERE, null, ex);

        return topWordsList;
    }

}

From source file:io.druid.indexer.BatchDeltaIngestionTest.java

@Test
public void testDeltaIngestion() throws Exception {
    File tmpDir = temporaryFolder.newFolder();

    File dataFile1 = new File(tmpDir, "data1");
    FileUtils.writeLines(dataFile1, ImmutableList.of("2014102200,a.example.com,a.example.com,90",
            "2014102201,b.example.com,b.example.com,25"));

    File dataFile2 = new File(tmpDir, "data2");
    FileUtils.writeLines(dataFile2, ImmutableList.of("2014102202,c.example.com,c.example.com,70"));

    //using a hadoop glob path to test that it continues to work with hadoop MultipleInputs usage and not
    //affected by
    //https://issues.apache.org/jira/browse/MAPREDUCE-5061
    String inputPath = tmpDir.getPath() + "/{data1,data2}";

    List<WindowedDataSegment> segments = ImmutableList.of(new WindowedDataSegment(SEGMENT, INTERVAL_FULL));

    HadoopDruidIndexerConfig config = makeHadoopDruidIndexerConfig(
            ImmutableMap.<String, Object>of("type", "multi", "children",
                    ImmutableList.of(//from  w  ww.jav a2s.com
                            ImmutableMap.<String, Object>of("type", "dataSource", "ingestionSpec",
                                    ImmutableMap.of("dataSource", "xyz", "interval", INTERVAL_FULL), "segments",
                                    segments),
                            ImmutableMap.<String, Object>of("type", "static", "paths", inputPath))),
            temporaryFolder.newFolder());

    List<ImmutableMap<String, Object>> expectedRows = ImmutableList.of(
            ImmutableMap.<String, Object>of("time", DateTime.parse("2014-10-22T00:00:00.000Z"), "host",
                    ImmutableList.of("a.example.com"), "visited_sum", 190L, "unique_hosts", 1.0d),
            ImmutableMap.<String, Object>of("time", DateTime.parse("2014-10-22T01:00:00.000Z"), "host",
                    ImmutableList.of("b.example.com"), "visited_sum", 175L, "unique_hosts", 1.0d),
            ImmutableMap.<String, Object>of("time", DateTime.parse("2014-10-22T02:00:00.000Z"), "host",
                    ImmutableList.of("c.example.com"), "visited_sum", 270L, "unique_hosts", 1.0d));

    testIngestion(config, expectedRows, Iterables.getOnlyElement(segments));
}

From source file:de.tudarmstadt.ukp.dkpro.tc.core.task.uima.ExtractFeaturesConnector.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    super.collectionProcessComplete();

    // apply filters that influence the whole feature store
    // filters are applied in the order that they appear as parameters
    for (String filterString : featureFilters) {
        FeatureStoreFilter filter;/*from ww  w .j  a  v  a2s  . c  om*/
        try {
            filter = (FeatureStoreFilter) Class.forName(filterString).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new AnalysisEngineProcessException(e);
        }

        if (filter.isApplicableForTraining() && !isTesting || filter.isApplicableForTesting() && isTesting) {
            filter.applyFilter(featureStore);
        }
    }

    // write feature names file if in training mode
    if (!isTesting) {
        try {
            FileUtils.writeLines(new File(outputDirectory, Constants.FILENAME_FEATURES),
                    featureStore.getFeatureNames());
        } catch (IOException e) {
            throw new AnalysisEngineProcessException(e);
        }
    }
    // apply the feature names filter
    else {
        File featureNamesFile = new File(outputDirectory, Constants.FILENAME_FEATURES);
        TreeSet<String> trainFeatureNames;
        try {
            trainFeatureNames = new TreeSet<>(FileUtils.readLines(featureNamesFile));
        } catch (IOException e) {
            throw new AnalysisEngineProcessException(e);
        }

        AdaptTestToTrainingFeaturesFilter filter = new AdaptTestToTrainingFeaturesFilter();
        // if feature space from training set and test set differs, apply the filter
        // to keep only features seen during training
        if (!trainFeatureNames.equals(featureStore.getFeatureNames())) {
            filter.setFeatureNames(trainFeatureNames);
            filter.applyFilter(featureStore);
        }
    }

    // FIXME if the feature store now determines whether to use dense or sparse instances, 
    // we might get rid of the corresponding parameter here
    // addInstanceId requires dense instances
    try {
        DataWriter writer = (DataWriter) Class.forName(dataWriterClass).newInstance();
        writer.write(outputDirectory, featureStore, true, learningMode, applyWeighting);
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    }
}

From source file:ml.shifu.shifu.core.processor.ModelDataEncodeProcessor.java

private void updateModel(String encodeRefModel) throws IOException {
    ModelConfig encodeModel = loadSubModelConfig(encodeRefModel);
    encodeModel.setModelSetName(encodeRefModel);

    int featureCnt = 1;
    for (int i = 0; i < this.treeModel.getTrees().size(); i++) {
        featureCnt = featureCnt * this.treeModel.getTrees().get(i).size();
    }//from w  w  w. j  av  a2s.c  o m

    List<String> categoricalVars = new ArrayList<String>();
    for (int i = 0; i < featureCnt; i++) {
        categoricalVars.add("tree_vars_" + i);
    }

    String catVarFileName = encodeModel.getDataSet().getCategoricalColumnNameFile();
    if (StringUtils.isBlank(catVarFileName)) {
        catVarFileName = "categorical.column.names";
        encodeModel.getDataSet().setCategoricalColumnNameFile(catVarFileName);
    }
    FileUtils.writeLines(new File(encodeRefModel + File.separator + catVarFileName), categoricalVars);

    saveModelConfig(encodeRefModel, encodeModel);
}

From source file:com.mythesis.profileanalysis.Utils.java

/**
 * Method that gets the results form sWebRank and puts them all together in the same file
 * @param inputDirectory the output directory of sWebRank
 * @param outputDirectory the directory where im gonna save the new file
 * @param file the name of the file where im gonna save the results e.g. xxx.txt
 *///from   ww w  .  j av a2  s .  c o  m
public void getLDAcontent(String inputDirectory, String outputDirectory, String file) {

    System.out.println("Getting webpages from " + inputDirectory);
    //Find output paths
    File root = new File(inputDirectory);
    File[] contents = root.listFiles();
    List<String> sWebRanklevels = new ArrayList<>();
    for (File f : contents) {
        if (f.getAbsolutePath().contains("level"))
            sWebRanklevels.add(f.getAbsolutePath());
    }

    //Find all query paths
    //for now this only works for bing search engine
    List<String> totalQueries = new ArrayList<>();
    for (String s : sWebRanklevels) {
        File level = new File(s);
        File[] queries = level.listFiles();
        for (File f : queries) {
            if (!f.getAbsolutePath().contains("txt"))
                totalQueries.add(f.getAbsolutePath() + "\\" + "bing" + "\\");

        }
    }

    int totalDocs = 0;
    //String totalContent="";
    List<String> totalContent = new ArrayList<>();
    String webpage = "";
    for (String s : totalQueries) {
        File level = new File(s);
        File[] docPaths = level.listFiles();
        for (File f : docPaths) {
            String str = f.getAbsolutePath();
            if (StringUtils.isNumeric(str.substring(str.lastIndexOf("\\") + 1))) {
                File webPagePath = new File(str + "\\html_parse_content.txt");
                try {
                    if (!directoryContains(f, webPagePath)) {
                        webPagePath = new File(f.getAbsolutePath() + "\\youtube_content.txt");
                        if (!directoryContains(f, webPagePath))
                            continue;
                        webpage = readFileToString(webPagePath).replace("\n", "").replace("\r", "");
                        YoutubeContent ytc = new YoutubeContent();
                        webpage = ytc.parse(webpage);
                    } else {
                        webpage = readFileToString(webPagePath);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (webpage.isEmpty())
                    continue;

                totalContent.add(webpage);
            }
        }
    }

    Set<String> hs = new HashSet<>();
    hs.addAll(totalContent);
    totalContent.clear();
    totalContent.addAll(hs);

    //find for each word the documents that appears in
    Map<String, HashSet<Integer>> wordsToSetOfDocsMap = new TreeMap<>();
    int size = totalContent.size();
    for (int d = 0; d < size; d++) {
        String doc = totalContent.get(d);
        String[] words = doc.trim().split(" ");
        int length = words.length;
        for (int w = 0; w < length; w++) {
            String word = words[w];
            if (!wordsToSetOfDocsMap.containsKey(word)) {
                wordsToSetOfDocsMap.put(word, new HashSet<Integer>());
            }
            HashSet<Integer> setOfDocs = wordsToSetOfDocsMap.get(word);
            setOfDocs.add(d);
            wordsToSetOfDocsMap.put(word, setOfDocs);
        }
    }

    //remove frequent words
    totalContent = removeFrequentWords(wordsToSetOfDocsMap, totalContent);

    //remove infrequent words
    totalContent = removeInfrequentWords(wordsToSetOfDocsMap, totalContent);

    //store results
    for (int i = 0; i < totalContent.size(); i++) {
        String content = totalContent.get(i).replaceAll("\\s+", " ").trim();
        totalContent.set(i, content);
    }
    totalDocs = totalContent.size();
    totalContent.add(0, String.valueOf(totalDocs));
    String LDAdirectory = outputDirectory;
    File ldaContent = new File(LDAdirectory + file);
    try {
        FileUtils.writeLines(ldaContent, totalContent);
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println("I stored the results in " + outputDirectory + file);
}

From source file:com.googlecode.jgenhtml.JGenHtmlTestUtils.java

/**
 * Get an lcovrc file, creating it if it doesn't exist.
 * @param cssFile/*from   w  w  w  .  j a v a 2 s  . c o  m*/
 * @param noPrefix
 * @param numSpaces
 * @param funcCoverage
 * @param branchCoverage
 * @return
 */
public static File createLcovrcFile(String cssFile, String noPrefix, String numSpaces, String funcCoverage,
        String branchCoverage, String sort, String noSource, String gzipHtml) {
    File lcovrc = new File(getTestDir(), "lcovrc");
    try {
        List<String> lines = new ArrayList<String>();
        //throw in a few comments and stuff
        lines.add("#");
        lines.add("#jgenthml test file");
        lines.add("#");
        lines.add("#genhtml_num_spaces = 2");
        lines.add("");
        if (cssFile != null) {
            lines.add("genhtml_css_file = " + cssFile);
        }
        lines.add("#genhtml_no_prefix = 1");
        lines.add("");
        if (noPrefix != null) {
            lines.add("genhtml_no_prefix = " + noPrefix);
        }
        if (numSpaces != null) {
            lines.add("genhtml_num_spaces = " + numSpaces);
        }
        if (funcCoverage != null) {
            lines.add("genhtml_function_coverage = " + funcCoverage);
        }
        if (branchCoverage != null) {
            lines.add("genhtml_branch_coverage = " + branchCoverage);
        }
        if (sort != null) {
            lines.add("genhtml_sort = " + sort);
        }
        if (noSource != null) {
            lines.add("genhtml_no_source=" + noSource);
        }
        lines.add("#genhtml_html_gzip=0");
        if (gzipHtml != null) {
            lines.add("genhtml_html_gzip=" + gzipHtml);
        }
        FileUtils.writeLines(lcovrc, lines);
    } catch (IOException ex) {
        Logger.getLogger(JGenHtmlTestUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return lcovrc;
}

From source file:com.amazonaws.eclipse.android.sdk.newproject.NewAndroidProjectWizard.java

@Override
@SuppressWarnings("restriction")
public boolean performFinish() {
    if (getContainer() instanceof WizardDialog) {
        setRunnableContext((WizardDialog) getContainer());
    }//from w  w w  .  j a  v a 2 s  . com

    try {
        NewProjectWizardState newProjectWizardState = new NewProjectWizardState(Mode.ANY);
        newProjectWizardState.projectName = dataModel.getProjectName();
        newProjectWizardState.applicationName = "AWS Android Application";
        newProjectWizardState.packageName = dataModel.getPackageName();
        newProjectWizardState.target = dataModel.getAndroidTarget();
        newProjectWizardState.createActivity = false;

        new NewProjectCreator(newProjectWizardState, runnableContext).createAndroidProjects();
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(dataModel.getProjectName());

        IJavaProject javaProject = JavaCore.create(project);
        AndroidSdkInstall awsAndroidSdk = AndroidSdkManager.getInstance().getDefaultSdkInstall();
        awsAndroidSdk.writeMetadataToProject(javaProject);
        AwsAndroidSdkClasspathUtils.addAwsAndroidSdkToProjectClasspath(javaProject, awsAndroidSdk);

        AndroidManifestFile androidManifestFile = new AndroidManifestFile(project);
        androidManifestFile.initialize();
        copyProguardPropertiesFile(project);

        if (dataModel.isSampleCodeIncluded()) {
            // copy sample code files over
            Bundle bundle = Platform.getBundle(AndroidSDKPlugin.PLUGIN_ID);
            URL url = FileLocator.find(bundle, new Path("resources/S3_Uploader/"), null);
            try {
                File sourceFile = new File(FileLocator.resolve(url).toURI());
                File projectFolder = project.getLocation().toFile();
                File projectSourceFolder = new File(projectFolder, "src");

                for (File file : sourceFile.listFiles()) {
                    File destinationFile = new File(project.getLocation().toFile(), file.getName());
                    if (file.isDirectory())
                        FileUtils.copyDirectory(file, destinationFile);
                    else
                        FileUtils.copyFile(file, destinationFile);
                }

                // move *.java files to new src dir
                String s = dataModel.getPackageName().replace(".", File.separator) + File.separator;
                for (File file : projectSourceFolder.listFiles()) {
                    if (file.isDirectory())
                        continue;
                    File destinationFile = new File(projectSourceFolder, s + file.getName());
                    FileUtils.moveFile(file, destinationFile);

                    // update package lines with regex
                    // replace "com.amazonaws.demo.s3uploader" with dataModel.getPackageName()
                    List<String> lines = FileUtils.readLines(destinationFile);
                    ArrayList<String> outLines = new ArrayList<String>();
                    for (String line : lines) {
                        outLines.add(line.replace("com.amazonaws.demo.s3uploader", dataModel.getPackageName()));
                    }
                    FileUtils.writeLines(destinationFile, outLines);
                }

                // update android manifest file
                androidManifestFile.addSampleActivity();
            } catch (Exception e) {
                IStatus status = new Status(IStatus.ERROR, AndroidSDKPlugin.PLUGIN_ID,
                        "Unable to update AWS SDK with sample app for Android project setup", e);
                StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
            }
        }

        // refresh the workspace to pick up the changes we just made
        project.refreshLocal(IResource.DEPTH_INFINITE, null);
    } catch (Exception e) {
        IStatus status = new Status(IStatus.ERROR, AndroidSDKPlugin.PLUGIN_ID,
                "Unable to create new AWS Android project", e);
        StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
        return false;
    }

    return true;
}

From source file:com.dubic.dc.dev.assist.services.ModuleService.java

public String processFile(String moduleName, boolean tfw, ModFile mfile) throws IOException {
    if (ModFile.MANUAL.equals(mfile.getAction())) {
        return "Edit manually";
    } else if (ModFile.DELETE.equals(mfile.getAction())) {
        FileUtils.deleteQuietly(new File(mfile.getPath()));
        System.out.println("delete file : " + new File(mfile.getName()));
        return "deleted";
    }//from  w  w w . j  av a  2s . com

    File f = new File(mfile.getPath());
    Map<String, Object> p = new HashMap<>();
    p.put(serviceKey, moduleName);
    p.put("tfw", tfw);
    String contents = resolveTemplate(p, mfile.getTemplate());

    if (ModFile.REPLACE.equals(mfile.getAction())) {
        FileUtils.write(f, contents);
        System.out.println("Replaced file : " + f.getName());
        return "replaced";
    } else if (ModFile.APPEND.equals(mfile.getAction())) {
        //check if line exists
        if (FileUtils.readFileToString(f).contains(mfile.getChecktext())) {
            System.out.println("EXISTS IN FILE!!! : " + f.getName());
            return "exists";
        }

        List<String> lines = FileUtils.readLines(f);
        boolean started = false;

        int i = 0;
        for (String line : lines) {
            //
            if (mfile.getStartline() != null) {//start line is set
                //                    System.out.println("mfile.getStartline() != null");
                if (line.contains(mfile.getStartline())) {//start line is hit
                    //                        System.out.println("line.equals(mfile.getStartline()");
                    started = true;
                }
                if (started && line.contains(mfile.getEndline())) {//end line hit
                    System.out.println("started && line.equals(mfile.getEndline())");
                    lines.add(i, contents);
                    break;
                }
            } else {//No start line so use end line
                if (mfile.getEndline() == null) {
                    lines.add(contents);
                    break;
                } else if (line.contains(mfile.getEndline())) {//end line hit
                    lines.add(i, contents);
                    break;
                }
            }
            i++;
        }

        //            for (String line : lines) {
        //                System.out.println(line);
        //            }
        FileUtils.writeLines(f, lines);
        System.out.println("Appended to file : " + f.getName());
        return "appended";

    } else if (ModFile.PREPEND.equals(mfile.getAction())) {
        if (FileUtils.readFileToString(f).contains(mfile.getChecktext())) {
            System.out.println("EXISTS IN FILE!!! : " + f.getName());
            return "exists";
        }

        List<String> originalLines = FileUtils.readLines(f);
        originalLines.add(0, contents);
        FileUtils.writeLines(f, originalLines);
        System.out.println("Prepended to file : " + f.getName());
        return "prepended";
    } else if (ModFile.REMOVE.equals(mfile.getAction())) {
        List<String> lines = FileUtils.readLines(f);
        int i = 0;
        for (String line : lines) {
            if (line.contains(contents)) {
                //                    exists?
                if (line.startsWith("//")) {
                    return "exists";
                }
                lines.remove(i);
                lines.add(i, "//".concat(line));
                break;
            }
            i++;
        }
        FileUtils.writeLines(f, lines);
        System.out.println("Commented out from : " + f.getName());
        return "commented";
    } else if (ModFile.CREATE.equals(mfile.getAction())) {
        FileUtils.write(f, contents, "UTF-8");
        System.out.println("Created file : " + f.getName());
        return "created";
    } else {
        System.out.println("No Action");
        return "no action";
    }
}

From source file:it.drwolf.ridire.utility.RIDIREReTagger.java

public String retagFile(File f) throws ExecuteException, IOException {
    // Map<String, File> map = new HashMap<String, File>();
    String fileIN = f.getAbsolutePath();
    String fileOut = f.getAbsolutePath() + ".iso";
    String posOld = f.getAbsolutePath() + ".iso.pos";
    String posNew = f.getAbsolutePath() + ".pos";
    // first convert from utf8 to iso8859-1
    CommandLine commandLine = CommandLine.parse("iconv");
    commandLine.addArgument("-c").addArgument("-s").addArgument("-f").addArgument("utf8").addArgument("-t")
            .addArgument("iso8859-1//TRANSLIT").addArgument("-o").addArgument(fileOut, false)
            .addArgument(fileIN, false);
    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT);
    executor.setWatchdog(watchdog);//  ww w .  jav a2 s .c om
    int exitValue = executor.execute(commandLine);
    if (exitValue == 0) {
        // tag using latin1 and Baroni's tagset
        commandLine = CommandLine.parse(this.treeTaggerBin);
        commandLine.addArgument(fileOut, false);
        executor = new DefaultExecutor();
        executor.setExitValue(0);
        watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT);
        executor.setWatchdog(watchdog);
        TreeTaggerLog treeTaggerLog = new TreeTaggerLog();
        PumpStreamHandler executeStreamHandler = new PumpStreamHandler(treeTaggerLog, null);
        executor.setStreamHandler(executeStreamHandler);
        int exitValue2 = executor.execute(commandLine);
        if (exitValue2 == 0) {
            // FileUtils.deleteQuietly(new File(fileOut));
            File posTagFile = new File(posOld);
            FileUtils.writeLines(posTagFile, treeTaggerLog.getLines());
        }
        // reconvert to utf8
        commandLine = CommandLine.parse("iconv");
        commandLine.addArgument("-s").addArgument("-f").addArgument("iso8859-1").addArgument("-t")
                .addArgument("utf8//TRANSLIT").addArgument("-o").addArgument(posNew, false)
                .addArgument(posOld, false);
        executor = new DefaultExecutor();
        watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT);
        executor.setWatchdog(watchdog);
        int exitValue3 = executor.execute(commandLine);
        if (exitValue3 == 0) {
            // FileUtils.deleteQuietly(new File(f.getPath() + ".iso.pos"));
            return new File(posNew).getCanonicalPath();
        }
    }
    return null;
}