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:org.archive.bdb.BdbModule.java

public void doCheckpoint(final Checkpoint checkpointInProgress) throws IOException {
    // First sync objectCaches
    for (@SuppressWarnings("rawtypes")
    ObjectIdentityCache oic : oiCaches.values()) {
        oic.sync();//from   w  w  w .j  av  a  2 s  . c  o m
    }

    try {
        // sync all databases
        for (DatabasePlusConfig dbc : databases.values()) {
            dbc.database.sync();
        }

        // Do a force checkpoint.  Thats what a sync does (i.e. doSync).
        CheckpointConfig chkptConfig = new CheckpointConfig();
        chkptConfig.setForce(true);

        // Mark Hayes of sleepycat says:
        // "The default for this property is false, which gives the current
        // behavior (allow deltas).  If this property is true, deltas are
        // prohibited -- full versions of internal nodes are always logged
        // during the checkpoint. When a full version of an internal node
        // is logged during a checkpoint, recovery does not need to process
        // it at all.  It is only fetched if needed by the application,
        // during normal DB operations after recovery. When a delta of an
        // internal node is logged during a checkpoint, recovery must
        // process it by fetching the full version of the node from earlier
        // in the log, and then applying the delta to it.  This can be
        // pretty slow, since it is potentially a large amount of
        // random I/O."
        // chkptConfig.setMinimizeRecoveryTime(true);
        bdbEnvironment.checkpoint(chkptConfig);
        LOGGER.fine("Finished bdb checkpoint.");

        DbBackup dbBackup = new DbBackup(bdbEnvironment);
        try {
            dbBackup.startBackup();

            File envCpDir = new File(dir.getFile(), checkpointInProgress.getName());
            org.archive.util.FileUtils.ensureWriteableDirectory(envCpDir);
            File logfilesList = new File(envCpDir, "jdbfiles.manifest");
            String[] filedata = dbBackup.getLogFilesInBackupSet();
            for (int i = 0; i < filedata.length; i++) {
                File f = new File(dir.getFile(), filedata[i]);
                filedata[i] += "," + f.length();
                if (getUseHardLinkCheckpoints()) {
                    File hardLink = new File(envCpDir, filedata[i]);
                    if (!FilesystemLinkMaker.makeHardLink(f.getAbsolutePath(), hardLink.getAbsolutePath())) {
                        LOGGER.log(Level.SEVERE, "unable to create required checkpoint link " + hardLink);
                    }
                }
            }
            FileUtils.writeLines(logfilesList, Arrays.asList(filedata));
            LOGGER.fine("Finished processing bdb log files.");
        } finally {
            dbBackup.endBackup();
        }
    } catch (DatabaseException e) {
        throw new IOException(e);
    }

    if (checkpointInProgress.getForgetAllButLatest()) {
        File[] oldEnvCpDirs = dir.getFile().listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return !name.equals(checkpointInProgress.getName())
                        && TextUtils.matches("cp\\d{5}-\\d{14}", name);
            }
        });
        for (File d : oldEnvCpDirs) {
            FileUtils.deleteDirectory(d);
        }
    }
}

From source file:org.bigtextml.topics.ParallelTopicModel.java

public void topicXMLReport(int iterationNo, int numWords) {
    List<String> lines = new ArrayList<String>();
    ArrayList<TreeSet<IDSorter>> topicSortedWords = getSortedWords();
    lines.add("<?xml version='1.0' ?>");
    lines.add("<topicModel>");
    for (int topic = 0; topic < numTopics; topic++) {
        lines.add("  <topic id='" + topic + "' alpha='" + alpha[topic] + "' totalTokens='"
                + tokensPerTopic[topic] + "'>");
        int word = 1;
        Iterator<IDSorter> iterator = topicSortedWords.get(topic).iterator();
        while (iterator.hasNext() && word < numWords) {
            IDSorter info = iterator.next();
            double weight = info.getWeight();
            if (weight > this.weightThreshold) {
                lines.add("   <word rank='" + word + "' " + "weight='" + weight + "'>"
                        + alphabet.lookupObject(info.getID()) + "</word>");

                word++;/*from ww w. ja va2 s  .co  m*/
            }
        }
        lines.add("  </topic>");
    }
    lines.add("</topicModel>");
    if (this.tDir != null) {
        try {
            if (tDir.exists()) {
                File f = new File(tDir.getAbsolutePath() + "/topicsAtIteration" + iterationNo + ".xml");
                logger.info("Writing Topic File at Iteration " + iterationNo + " to path ==="
                        + f.getAbsolutePath());
                FileUtils.writeLines(f, lines);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        logger.info("Writing Topic File at Iteration " + iterationNo + " System.out------");
        for (String l : lines) {
            System.out.println(l);
        }
        System.out.println("\n\n");
    }

}

From source file:org.broadinstitute.gatk.utils.interval.IntervalUtilsUnitTest.java

@Test(dataProvider = "flankingIntervalsFiles")
public void testWriteFlankingIntervals(FlankingIntervalsTestData data) throws Exception {
    File originalFile = createTempFile("original.", ".intervals");
    File flankingFile = createTempFile("flanking.", ".intervals");
    try {//from w  w  w.j  a va  2 s .c  o  m
        List<String> lines = new ArrayList<String>();
        for (GenomeLoc loc : data.original)
            lines.add(loc.toString());
        FileUtils.writeLines(originalFile, lines);

        IntervalUtils.writeFlankingIntervals(data.referenceFile, originalFile, flankingFile, data.basePairs);

        List<GenomeLoc> actual = IntervalUtils.intervalFileToList(data.parser, flankingFile.getAbsolutePath());

        String description = String.format("%n      name: %s%n  original: %s%n    actual: %s%n  expected: %s%n",
                data.toString(), data.original, actual, data.expected);
        Assert.assertEquals(actual, data.expected, description);
    } finally {
        FileUtils.deleteQuietly(originalFile);
        FileUtils.deleteQuietly(flankingFile);
    }
}

From source file:org.broadinstitute.gatk.utils.interval.IntervalUtilsUnitTest.java

@Test(dataProvider = "flankingIntervalsLists", expectedExceptions = UserException.class)
public void testWritingBadFlankingIntervals(FlankingIntervalsTestData data) throws Exception {
    File originalFile = createTempFile("original.", ".intervals");
    File flankingFile = createTempFile("flanking.", ".intervals");
    try {/*from   ww w  .j  a va  2  s.  c o m*/
        List<String> lines = new ArrayList<String>();
        for (GenomeLoc loc : data.original)
            lines.add(loc.toString());
        FileUtils.writeLines(originalFile, lines);

        // Should throw a user exception on bad input if either the original
        // intervals are empty or if the flanking intervals are empty
        IntervalUtils.writeFlankingIntervals(data.referenceFile, originalFile, flankingFile, data.basePairs);
    } finally {
        FileUtils.deleteQuietly(originalFile);
        FileUtils.deleteQuietly(flankingFile);
    }
}

From source file:org.broadinstitute.gatk.utils.interval.IntervalUtilsUnitTest.java

private File createTempFile(String tempFilePrefix, String tempFileExtension, String... lines) throws Exception {
    File tempFile = BaseTest.createTempFile(tempFilePrefix, tempFileExtension);
    FileUtils.writeLines(tempFile, Arrays.asList(lines));
    return tempFile;
}

From source file:org.canova.api.records.reader.impl.LineReaderTest.java

@Test
public void testLineReader() throws Exception {
    File tmpdir = new File("tmpdir");
    tmpdir.mkdir();/*from   w w w .  jav  a2  s  .co m*/

    File tmp1 = new File("tmpdir/tmp1.txt");
    File tmp2 = new File("tmpdir/tmp2.txt");
    File tmp3 = new File("tmpdir/tmp3.txt");

    FileUtils.writeLines(tmp1, Arrays.asList("1", "2", "3"));
    FileUtils.writeLines(tmp2, Arrays.asList("4", "5", "6"));
    FileUtils.writeLines(tmp3, Arrays.asList("7", "8", "9"));

    InputSplit split = new FileSplit(tmpdir);

    RecordReader reader = new LineRecordReader();
    reader.initialize(split);

    int count = 0;
    while (reader.hasNext()) {
        assertEquals(1, reader.next().size());
        count++;
    }

    assertEquals(9, count);

    FileUtils.deleteDirectory(tmpdir);
}

From source file:org.clickframes.util.CodeGenerator.java

public static void storeChecksumInFile(File file) throws IOException {
    long checksum = checksum(FileUtils.readFileToString(file));

    @SuppressWarnings("unchecked")
    List<String> lines = FileUtils.readLines(file);

    // add checksum at the end of the file
    lines.add(commentForFilename(file.getName(), "version=" + checksum));

    FileUtils.writeLines(file, lines);
}

From source file:org.commonjava.aprox.subsys.datafile.DataFile.java

public void writeLines(final List<String> lines, final ChangeSummary summary) throws IOException {
    FileUtils.writeLines(file, lines);
    events.modified(file, summary);//from  ww w  .j a v  a 2s. c  o m
}

From source file:org.darkstar.batch.TextIdUpdate.java

/**
 * Method to Update Text IDs for a Single Zone
 * @param zoneId Zone ID to Update/*from  www. j  a  v  a  2  s .c om*/
 */
private void updateTextIdsForZone(final int zoneId) {
    final String textIdFilePath = DarkstarUtils.getTextIdFilePath(configProperties, zoneId);
    final File textIdFile = new File(textIdFilePath);
    final File polUtilsDialogTableFile = new File(
            DarkstarUtils.getDialogTablePathByZoneId(configProperties, zoneId));
    ;

    // Read the Dialog Table File, and Keep Both & Original Read of it and a Filtered Read
    String polUtilsString;

    try {
        // Shifting Encoding to Avoid Issues w/ Regex Replace All
        polUtilsString = FileUtils.readFileToString(polUtilsDialogTableFile, "Shift_JIS");
        polUtilsString = DarkstarUtils.collapseAndFormatXmlString(polUtilsString);
        byte[] polUtilsShiftJisBytes = polUtilsString.getBytes("Shift_JIS");
        Charset shiftJis = Charset.forName("Shift_JIS");
        Charset ansi = Charset.forName("Cp1252");
        CharBuffer shiftJisCharBuffer = shiftJis.decode(ByteBuffer.wrap(polUtilsShiftJisBytes));
        ByteBuffer ansiByteBuffer = ansi.encode(shiftJisCharBuffer);
        polUtilsString = new String(ansiByteBuffer.array(), "Cp1252");
    } catch (IOException e) {
        LOG.error(String.format("Zone ID <%d> -> Could Not Read Dialog Table, Skipping...", zoneId), e);
        errors++;
        return;
    }

    final String filteredPolUtilsString = DarkstarUtils.filterBadCharacters(polUtilsString);

    // Optional: Write Filtered Dialog Tables. Helps you figure out how you can make a TextID auto-update in the future.
    if (writeFilteredDialogTables) {
        DarkstarUtils.writeFilteredDialogTable(configProperties, filteredPolUtilsString, zoneId);
    }

    // Read the Text ID File
    List<String> textIdLines;

    try {
        textIdLines = FileUtils.readLines(textIdFile, "UTF-8");
    } catch (IOException e) {
        LOG.error(String.format("Zone ID <%d> -> Could Not Read Text ID File, Skipping...", zoneId), e);
        errors++;
        return;
    }

    int zoneGuesses = 0;

    for (int lineIndex = 0; lineIndex < textIdLines.size(); lineIndex++) {
        String textIdLine = textIdLines.get(lineIndex);

        // If we don't have all 3 of these markers on a line, then the line is not a text id definition.
        if (textIdLine.indexOf("=") == -1 || textIdLine.indexOf(";") == -1) {
            continue;
        }

        String comment = "";

        // Search for Markers on this Line
        final int lineEqual = textIdLine.indexOf("=");
        final int semiColon = textIdLine.indexOf(";", lineEqual);

        // Capture the Comment
        final String variable = textIdLine.substring(0, lineEqual).trim();

        // Capture the Current Id
        final int id = Integer.valueOf(textIdLine.substring(lineEqual + 1, semiColon).trim());

        boolean appendComment = false;

        if (textIdLine.indexOf("--") >= 0) {
            final int commentIndex = textIdLine.indexOf("--");
            //    Capture the Comment         
            comment = textIdLine.substring(commentIndex + 2).trim();
        } else {
            comment = configProperties.getProperty(variable);
            if (comment == null) {
                comment = "@UNKNOWN_GLOBAL@";
            } else {
                appendComment = true;
            }
        }

        // Ignore Unknowns & Current ID 0's
        if ("??? message".equals(comment) || "[UNKNOWN]".equals(comment) || id == 0) {
            continue;
        }

        // Apply some basic filtering. These are showing up as a single ? in POLUtils right now,
        // while they are filled out in most of our Text ID Luas. So we need to sync up before
        // comparing. Since this is very common, we will not treat this as triggering a guess.
        String searchComment = comment;
        searchComment = searchComment.replaceAll("(<<<)", "<");
        searchComment = searchComment.replaceAll("(>>>)", ">");
        searchComment = searchComment.replace('<', '?');
        searchComment = searchComment.replace('>', '?');

        // Hack for Case Mismatching on This, Us Vs. POLUtils in a lot of cases
        if ("CHEST_FAIL".equals(variable)) {
            searchComment = searchComment.toLowerCase(Locale.US);
        }

        // We're going to capture where the current position in Dialog Utils, and start our search from there. 
        // Helps keep the right ID when there a multiple similar lines
        final String lastSearchIndexString = DarkstarUtils.FIELD_INDEX_OPENING_TAG + id
                + DarkstarUtils.FIELD_CLOSING_TAG;
        int lastIndex = polUtilsString.indexOf(lastSearchIndexString);
        int lastFilteredIndex = filteredPolUtilsString.indexOf(lastSearchIndexString);

        // Run initial search. If this matches, its considered a match & not a guess.
        boolean guess = false;
        boolean filtered = false;
        LOG.debug("Searching: " + searchComment);

        int polUtilsIndex = polUtilsString.indexOf(searchComment, lastIndex);

        // If we couldn't find it, we'll have to apply bad char filtering
        if (polUtilsIndex == -1) {
            searchComment = DarkstarUtils.filterBadCharacters(searchComment).trim();
            LOG.debug("Searching (Filtered): " + searchComment);
            polUtilsIndex = filteredPolUtilsString.indexOf(searchComment, lastFilteredIndex);
            guess = false;
            filtered = true;
        }

        // If we couldn't find it again, see if there's a manually specified version in the properties file
        if (polUtilsIndex == -1) {
            searchComment = configProperties.getProperty(variable);

            if (searchComment != null) {
                LOG.debug("Searching (Manual): " + searchComment);
                polUtilsIndex = filteredPolUtilsString.indexOf(searchComment, lastFilteredIndex);
                guess = false; // Doesn't count as a guess since someone manually set the criteria
                filtered = true;
            }
        }

        // If we've found something...
        if (polUtilsIndex >= 0) {
            // Capture the New ID Value
            int fieldIndex;
            int fieldEndIndex;
            int fieldValue;

            if (filtered) {
                fieldIndex = filteredPolUtilsString.lastIndexOf(DarkstarUtils.FIELD_INDEX_OPENING_TAG,
                        polUtilsIndex);
                fieldEndIndex = filteredPolUtilsString.indexOf(DarkstarUtils.FIELD_CLOSING_TAG, fieldIndex);
                fieldValue = Integer.valueOf(filteredPolUtilsString
                        .substring(fieldIndex + DarkstarUtils.FIELD_INDEX_OPENING_TAG.length(), fieldEndIndex));
            } else {
                fieldIndex = polUtilsString.lastIndexOf(DarkstarUtils.FIELD_INDEX_OPENING_TAG, polUtilsIndex);
                fieldEndIndex = polUtilsString.indexOf(DarkstarUtils.FIELD_CLOSING_TAG, fieldIndex);
                fieldValue = Integer.valueOf(polUtilsString
                        .substring(fieldIndex + DarkstarUtils.FIELD_INDEX_OPENING_TAG.length(), fieldEndIndex));
            }

            // If the new Id & Old Id are the same, leave it alone
            if (id == fieldValue) {
                guess = false;
                LOG.debug(String.format("%s: %d -> %d, Same Id Detected, Leaving it alone...", variable, id,
                        fieldValue));
            }
            // Update it
            else {
                textIdLine = textIdLine.replaceAll("(" + id + ")", String.valueOf(fieldValue));

                if (appendComment) {
                    textIdLine = textIdLine + " -- " + comment;
                }

                textIdLines.set(lineIndex, textIdLine);
                LOG.info(String.format("%s: %d -> %d", variable, id, fieldValue));
            }
        }
        // If we've got nothing and we're marking stuff, flag it with FIXME for manual review. Otherwise we're leaving it alone.
        else if (markGuesses) {
            textIdLine = DarkstarUtils.FIX_ME + textIdLine;
            textIdLines.set(lineIndex, textIdLine);
            LOG.info(String.format(
                    "%s: %d -> ???, Couldn't Find Any Matches, Marking for Manual Review With FIXME...",
                    variable, id));
            guess = true;
        } else {
            LOG.info(String.format("%s: %d -> ???, Couldn't Find Any Matches, So We Left it Alone...", variable,
                    id));
        }

        if (guess) {
            zoneGuesses++;
        }
    }

    // Write Text ID File
    try {
        FileUtils.writeLines(textIdFile, textIdLines);
    } catch (IOException e) {
        LOG.error(String.format("Zone ID <%d> -> Could Write Text ID File, Skipping...", zoneId), e);
        errors++;
        return;
    }

    // Record Fixme / Guess Data
    fixmeCountMap.put(textIdFilePath, zoneGuesses);
    guesses += zoneGuesses;

}

From source file:org.darkstar.batch.utils.DarkstarUtils.java

/**
 * Method to Save the NPC List SQL File//  w  w  w .j  a v a 2 s  . com
 * @param configProperties Handle to Batch Configuration Properties
 * @param npcListSqlLines Array of the NPC List SQL Lines to Save
 */
public static void saveNpcListSqlFile(final Properties configProperties, final List<String> npcListSqlLines) {
    final StringBuilder npcListSqlPathBuilder = new StringBuilder();
    npcListSqlPathBuilder.append(configProperties.getProperty("darkstar_root", ""));
    npcListSqlPathBuilder.append("sql/npc_list.sql");

    final File npcIdFile = new File(npcListSqlPathBuilder.toString());

    try {
        FileUtils.writeLines(npcIdFile, npcListSqlLines);
    } catch (IOException e) {
        throw new RuntimeException("Failed to Write Npc List SQL File!", e);
    }
}