Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

In this page you can find the example usage for java.util Scanner close.

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:com.santhoshknn.sudoku.GridExtractor.java

/**
 * <p>/* www  .j a  v a  2 s  .  com*/
 * Parses the supplied file to extract a 9x9 grid of integers substituting
 * the supplied x with a 0
 * </p>
 * <b>Note:</b>Used internally for testing with various data. REST API uses
 * the operation above
 *
 * @param input
 * @return extracted grid if valid input, null otherwise
 */
public GridResponse parseFromFile(final String fileName) {
    int[][] grid = new int[9][9]; // default 0 vals
    GridResponse response = new GridResponse();
    Scanner scanner = null;
    String error = null;
    try {
        URL url = getClass().getResource(fileName);
        log.info("Reading input file [{}]", url.getFile());
        scanner = new Scanner(new File(url.getPath()));
        int row = 0;
        while (scanner.hasNext()) {
            int col = 0;
            String line = scanner.nextLine();
            // delete whitespaces added for cosmetic purpose
            line = StringUtils.deleteWhitespace(line);
            if (line.isEmpty())
                continue; // Sanitize input. Remove line added for
                          // readability
                          // fail if line's length!=9
            if (line.length() != 9) {
                error = INVALID_CHARS_IN_FILE + ":" + (row + 1);
                break;
            }

            for (int i = 0; i < line.length(); i++) {
                //log.info("Row [{}] Char is [{}]",row,line.charAt(i));
                if (Character.isDigit(line.charAt(i))) {
                    int number = Character.getNumericValue(line.charAt(i));
                    grid[row][col] = number;
                } else {
                    grid[row][col] = 0;
                }
                col++;
            }
            if (row == 9)
                break;
            row++;
        }
    } catch (FileNotFoundException e) {
        log.error("Error reading file [{}]", fileName, e);
    } finally {
        if (scanner != null)
            scanner.close();
    }
    if (null == error) {
        response.setGrid(grid);
    } else {
        response.setError(error);
        log.error(error);
    }
    return response;
}

From source file:com.joliciel.talismane.stats.FScoreCalculator.java

/**
 * Combine the results of n cross validation results into a single f-score file.
 * @param directory/* ww w  .  j  av  a  2s  . c  o m*/
 * @param prefix
 * @param suffix
 * @param csvFileWriter
 */
static void combineCrossValidationResults(File directory, String prefix, String suffix, Writer csvFileWriter) {
    try {
        File[] files = directory.listFiles();
        Map<Integer, Map<String, FScoreStats>> fileStatsMap = new HashMap<Integer, Map<String, FScoreStats>>();
        for (File file : files) {
            if (file.getName().startsWith(prefix) && file.getName().endsWith(suffix)) {
                int index = Integer.parseInt(file.getName().substring(prefix.length(), prefix.length() + 1));
                Map<String, FScoreStats> statsMap = new HashMap<String, FScoreCalculator.FScoreStats>();
                fileStatsMap.put(index, statsMap);
                Scanner scanner = new Scanner(
                        new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")));

                boolean firstLine = true;
                int truePositivePos = -1;

                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    List<String> cells = CSV.getCSVCells(line);
                    if (firstLine) {
                        int i = 0;
                        for (String cell : cells) {
                            if (cell.equals("true+")) {
                                truePositivePos = i;
                                break;
                            }
                            i++;
                        }
                        if (truePositivePos < 0) {
                            throw new JolicielException("Couldn't find true+ on first line");
                        }
                        firstLine = false;
                    } else {
                        FScoreStats stats = new FScoreStats();
                        String outcome = cells.get(0);
                        stats.outcome = outcome;
                        if (outcome.equals("AVERAGE"))
                            break;
                        stats.truePos = Integer.parseInt(cells.get(truePositivePos));
                        stats.falsePos = Integer.parseInt(cells.get(truePositivePos + 1));
                        stats.falseNeg = Integer.parseInt(cells.get(truePositivePos + 2));
                        stats.precision = Double.parseDouble(cells.get(truePositivePos + 3));
                        stats.recall = Double.parseDouble(cells.get(truePositivePos + 4));
                        stats.fScore = Double.parseDouble(cells.get(truePositivePos + 5));
                        statsMap.put(outcome, stats);
                    } // firstLine?
                } // has more lines
                scanner.close();
            } // file in current series
        } // next file

        int numFiles = fileStatsMap.size();
        if (numFiles == 0) {
            throw new JolicielException("No files found matching prefix and suffix provided");
        }
        Map<String, DescriptiveStatistics> descriptiveStatsMap = new HashMap<String, DescriptiveStatistics>();
        Map<String, FScoreStats> outcomeStats = new HashMap<String, FScoreCalculator.FScoreStats>();
        Set<String> outcomes = new TreeSet<String>();
        for (Map<String, FScoreStats> statsMap : fileStatsMap.values()) {
            for (FScoreStats stats : statsMap.values()) {
                DescriptiveStatistics fScoreStats = descriptiveStatsMap.get(stats.outcome + "fScore");
                if (fScoreStats == null) {
                    fScoreStats = new DescriptiveStatistics();
                    descriptiveStatsMap.put(stats.outcome + "fScore", fScoreStats);
                }
                fScoreStats.addValue(stats.fScore);
                DescriptiveStatistics precisionStats = descriptiveStatsMap.get(stats.outcome + "precision");
                if (precisionStats == null) {
                    precisionStats = new DescriptiveStatistics();
                    descriptiveStatsMap.put(stats.outcome + "precision", precisionStats);
                }
                precisionStats.addValue(stats.precision);
                DescriptiveStatistics recallStats = descriptiveStatsMap.get(stats.outcome + "recall");
                if (recallStats == null) {
                    recallStats = new DescriptiveStatistics();
                    descriptiveStatsMap.put(stats.outcome + "recall", recallStats);
                }
                recallStats.addValue(stats.recall);

                FScoreStats outcomeStat = outcomeStats.get(stats.outcome);
                if (outcomeStat == null) {
                    outcomeStat = new FScoreStats();
                    outcomeStat.outcome = stats.outcome;
                    outcomeStats.put(stats.outcome, outcomeStat);
                }
                outcomeStat.truePos += stats.truePos;
                outcomeStat.falsePos += stats.falsePos;
                outcomeStat.falseNeg += stats.falseNeg;

                outcomes.add(stats.outcome);
            }
        }

        csvFileWriter.write(CSV.format(prefix + suffix));
        csvFileWriter.write("\n");
        csvFileWriter.write(CSV.format("outcome"));
        csvFileWriter.write(CSV.format("true+") + CSV.format("false+") + CSV.format("false-")
                + CSV.format("tot precision") + CSV.format("avg precision") + CSV.format("dev precision")
                + CSV.format("tot recall") + CSV.format("avg recall") + CSV.format("dev recall")
                + CSV.format("tot f-score") + CSV.format("avg f-score") + CSV.format("dev f-score") + "\n");

        for (String outcome : outcomes) {
            csvFileWriter.write(CSV.format(outcome));
            FScoreStats outcomeStat = outcomeStats.get(outcome);
            DescriptiveStatistics fScoreStats = descriptiveStatsMap.get(outcome + "fScore");
            DescriptiveStatistics precisionStats = descriptiveStatsMap.get(outcome + "precision");
            DescriptiveStatistics recallStats = descriptiveStatsMap.get(outcome + "recall");
            outcomeStat.calculate();
            csvFileWriter.write(CSV.format(outcomeStat.truePos));
            csvFileWriter.write(CSV.format(outcomeStat.falsePos));
            csvFileWriter.write(CSV.format(outcomeStat.falseNeg));
            csvFileWriter.write(CSV.format(outcomeStat.precision * 100));
            csvFileWriter.write(CSV.format(precisionStats.getMean()));
            csvFileWriter.write(CSV.format(precisionStats.getStandardDeviation()));
            csvFileWriter.write(CSV.format(outcomeStat.recall * 100));
            csvFileWriter.write(CSV.format(recallStats.getMean()));
            csvFileWriter.write(CSV.format(recallStats.getStandardDeviation()));
            csvFileWriter.write(CSV.format(outcomeStat.fScore * 100));
            csvFileWriter.write(CSV.format(fScoreStats.getMean()));
            csvFileWriter.write(CSV.format(fScoreStats.getStandardDeviation()));
            csvFileWriter.write("\n");
            csvFileWriter.flush();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:azkaban.viewer.reportal.ReportalMailCreator.java

private boolean createMessage(Project project, ExecutableFlow flow, EmailMessage message, String urlPrefix,
        boolean printData) throws Exception {
    message.println("<html>");
    message.println("<head></head>");
    message.println(/*from w  w w .j  ava  2  s. co m*/
            "<body style='font-family: verdana; color: #000000; background-color: #cccccc; padding: 20px;'>");
    message.println(
            "<div style='background-color: #ffffff; border: 1px solid #aaaaaa; padding: 20px;-webkit-border-radius: 15px; -moz-border-radius: 15px; border-radius: 15px;'>");
    // Title
    message.println("<b>" + project.getMetadata().get("title") + "</b>");
    message.println("<div style='font-size: .8em; margin-top: .5em; margin-bottom: .5em;'>");
    // Status
    message.println(flow.getStatus().name());
    // Link to logs
    message.println("(<a href='" + urlPrefix + "?view&logs&id=" + flow.getProjectId() + "&execid="
            + flow.getExecutionId() + "'>Logs</a>)");
    // Link to Data
    message.println("(<a href='" + urlPrefix + "?view&id=" + flow.getProjectId() + "&execid="
            + flow.getExecutionId() + "'>Result data</a>)");
    // Link to Edit
    message.println("(<a href='" + urlPrefix + "?edit&id=" + flow.getProjectId() + "'>Edit</a>)");
    message.println("</div>");
    message.println("<div style='margin-top: .5em; margin-bottom: .5em;'>");
    // Description
    message.println(project.getDescription());
    message.println("</div>");

    // Print variable values, if any
    Map<String, String> flowParameters = flow.getExecutionOptions().getFlowParameters();
    int i = 0;
    while (flowParameters.containsKey("reportal.variable." + i + ".from")) {
        if (i == 0) {
            message.println(
                    "<div style='margin-top: 10px; margin-bottom: 10px; border-bottom: 1px solid #ccc; padding-bottom: 5px; font-weight: bold;'>");
            message.println("Variables");
            message.println("</div>");
            message.println("<table border='1' cellspacing='0' cellpadding='2' style='font-size: 14px;'>");
            message.println("<thead><tr><th><b>Name</b></th><th><b>Value</b></th></tr></thead>");
            message.println("<tbody>");
        }

        message.println("<tr>");
        message.println("<td>" + flowParameters.get("reportal.variable." + i + ".from") + "</td>");
        message.println("<td>" + flowParameters.get("reportal.variable." + i + ".to") + "</td>");
        message.println("</tr>");

        i++;
    }

    if (i > 0) { // at least one variable
        message.println("</tbody>");
        message.println("</table>");
    }

    if (printData) {
        String locationFull = (outputLocation + "/" + flow.getExecutionId()).replace("//", "/");

        IStreamProvider streamProvider = ReportalUtil.getStreamProvider(outputFileSystem);

        if (streamProvider instanceof StreamProviderHDFS) {
            StreamProviderHDFS hdfsStreamProvider = (StreamProviderHDFS) streamProvider;
            hdfsStreamProvider.setHadoopSecurityManager(hadoopSecurityManager);
            hdfsStreamProvider.setUser(reportalStorageUser);
        }

        // Get file list
        String[] fileList = ReportalHelper.filterCSVFile(streamProvider.getFileList(locationFull));

        // Sort files in execution order.
        // File names are in the format {EXECUTION_ORDER}-{QUERY_TITLE}.csv
        // E.g.: 1-queryTitle.csv
        Arrays.sort(fileList, new Comparator<String>() {
            public int compare(String a, String b) {
                Integer aExecutionOrder = Integer.parseInt(a.substring(0, a.indexOf('-')));
                Integer bExecutionOrder = Integer.parseInt(b.substring(0, b.indexOf('-')));
                return aExecutionOrder.compareTo(bExecutionOrder);
            }

            public boolean equals(Object obj) {
                return this.equals(obj);
            }
        });

        // Get jobs in execution order
        List<ExecutableNode> jobs = ReportalUtil.sortExecutableNodes(flow);

        File tempFolder = new File(reportalMailTempDirectory + "/" + flow.getExecutionId());
        tempFolder.mkdirs();

        // Copy output files from HDFS to local disk, so you can send them as email attachments
        for (String file : fileList) {
            String filePath = locationFull + "/" + file;
            InputStream csvInputStream = null;
            OutputStream tempOutputStream = null;
            File tempOutputFile = new File(tempFolder, file);
            tempOutputFile.createNewFile();
            try {
                csvInputStream = streamProvider.getFileInputStream(filePath);
                tempOutputStream = new BufferedOutputStream(new FileOutputStream(tempOutputFile));

                IOUtils.copy(csvInputStream, tempOutputStream);
            } finally {
                IOUtils.closeQuietly(tempOutputStream);
                IOUtils.closeQuietly(csvInputStream);
            }
        }

        try {
            streamProvider.cleanUp();
        } catch (IOException e) {
            e.printStackTrace();
        }

        boolean emptyResults = true;

        for (i = 0; i < fileList.length; i++) {
            String file = fileList[i];
            ExecutableNode job = jobs.get(i);
            job.getAttempt();

            message.println(
                    "<div style='margin-top: 10px; margin-bottom: 10px; border-bottom: 1px solid #ccc; padding-bottom: 5px; font-weight: bold;'>");
            message.println(file);
            message.println("</div>");
            message.println("<div>");
            message.println("<table border='1' cellspacing='0' cellpadding='2' style='font-size: 14px;'>");
            File tempOutputFile = new File(tempFolder, file);
            InputStream csvInputStream = null;
            try {
                csvInputStream = new BufferedInputStream(new FileInputStream(tempOutputFile));
                Scanner rowScanner = new Scanner(csvInputStream);
                int lineNumber = 0;
                while (rowScanner.hasNextLine() && lineNumber <= NUM_PREVIEW_ROWS) {
                    // For Hive jobs, the first line is the column names, so we ignore it
                    // when deciding whether the output is empty or not
                    if (!job.getType().equals(ReportalType.HiveJob.getJobTypeName()) || lineNumber > 0) {
                        emptyResults = false;
                    }

                    String csvLine = rowScanner.nextLine();
                    String[] data = csvLine.split("\",\"");
                    message.println("<tr>");
                    for (String item : data) {
                        String column = StringEscapeUtils.escapeHtml(item.replace("\"", ""));
                        message.println("<td>" + column + "</td>");
                    }
                    message.println("</tr>");
                    if (lineNumber == NUM_PREVIEW_ROWS && rowScanner.hasNextLine()) {
                        message.println("<tr>");
                        message.println("<td colspan=\"" + data.length + "\">...</td>");
                        message.println("</tr>");
                    }
                    lineNumber++;
                }
                rowScanner.close();
                message.println("</table>");
                message.println("</div>");
            } finally {
                IOUtils.closeQuietly(csvInputStream);
            }
            message.addAttachment(file, tempOutputFile);
        }

        // Don't send an email if there are no results, unless this is an unscheduled run.
        String unscheduledRun = flowParameters.get("reportal.unscheduled.run");
        boolean isUnscheduledRun = unscheduledRun != null && unscheduledRun.trim().equalsIgnoreCase("true");
        if (emptyResults && !isUnscheduledRun) {
            return false;
        }
    }

    message.println("</div>").println("</body>").println("</html>");

    return true;
}

From source file:com.mirth.connect.server.migration.Migrator.java

protected List<String> readStatements(String scriptResourceName, Map<String, Object> replacements)
        throws IOException {
    List<String> script = new ArrayList<String>();
    Scanner scanner = null;

    if (scriptResourceName.charAt(0) != '/' && defaultScriptPath != null) {
        scriptResourceName = defaultScriptPath + "/" + scriptResourceName;
    }//from   ww w  .  ja  va  2 s .co m

    try {
        scanner = new Scanner(IOUtils.toString(ResourceUtil.getResourceStream(getClass(), scriptResourceName)));

        while (scanner.hasNextLine()) {
            StringBuilder stringBuilder = new StringBuilder();
            boolean blankLine = false;

            while (scanner.hasNextLine() && !blankLine) {
                String temp = scanner.nextLine();

                if (temp.trim().length() > 0) {
                    stringBuilder.append(temp + " ");
                } else {
                    blankLine = true;
                }
            }

            // Trim ending semicolons so Oracle doesn't throw
            // "java.sql.SQLException: ORA-00911: invalid character"
            String statementString = StringUtils.removeEnd(stringBuilder.toString().trim(), ";");

            if (statementString.length() > 0) {
                if (replacements != null && !replacements.isEmpty()) {
                    for (String key : replacements.keySet()) {
                        statementString = StringUtils.replace(statementString, "${" + key + "}",
                                replacements.get(key).toString());
                    }
                }

                script.add(statementString);
            }
        }

        return script;
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:ch.puzzle.itc.mobiliar.business.deploy.boundary.DeploymentBoundary.java

public String getDeploymentLog(String logName) throws IllegalAccessException {
    String logsPath = ConfigurationService.getProperty(ConfigKey.LOGS_PATH);
    if (logName.contains(File.separator)) {
        throw new IllegalAccessException("The log file contains a file separator (\"" + File.separator
                + "\"). For security reasons, this is not permitted!");
    }//  w  w  w . ja v  a  2 s . co  m

    StringBuilder content = new StringBuilder();
    Scanner scanner;
    try {
        scanner = new Scanner(new FileInputStream(logsPath + File.separator + logName));
        try {
            while (scanner.hasNextLine()) {
                content.append(scanner.nextLine()).append('\n');
            }
        } finally {
            scanner.close();
        }
        return content.toString();
    } catch (FileNotFoundException e) {
        String message = "The file " + logsPath + File.separator + logName
                + " was found, but couldn't be read!";
        log.log(Level.WARNING, message);
        throw new AMWRuntimeException(message, e);
    }

}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Convert file to string. Used on FileHandle
 * @param fileRead//from   w  w w.j ava2 s. c  o m
 * @return
 * @throws FileNotFoundException
 */
public String convertFileToString(File fileRead) throws FileNotFoundException {
    Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z");
    String readContent = scanner.next();
    scanner.close();

    return readContent;
}

From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMClusters.java

/**
 * Load the histogram from the file. Assumes the histogram is [int, float] format and creates a contiguous histogram
 * from zero//from   w w  w . j  a v  a 2s  .c  om
 * 
 * @param filename
 * @return
 */
private HistogramData loadHistogram(String filename) {
    BufferedReader input = null;
    try {
        int f = 0;
        double a = 0;
        String u = "";

        FileInputStream fis = new FileInputStream(filename);
        input = new BufferedReader(new UnicodeReader(fis, null));

        String line;
        int count = 0;

        ArrayList<float[]> data = new ArrayList<float[]>();

        // Read the header and store the calibration if present
        while ((line = input.readLine()) != null) {
            count++;
            if (line.length() == 0)
                continue;
            if (Character.isDigit(line.charAt(0)))
                // This is the first record
                break;
            String[] fields = line.split("[\t, ]+");
            if (fields[0].equalsIgnoreCase("frames"))
                f = Integer.parseInt(fields[1]);
            if (fields[0].equalsIgnoreCase("area"))
                a = Double.parseDouble(fields[1]);
            if (fields[0].equalsIgnoreCase("units"))
                u = fields[1];
        }

        final Pattern pattern = Pattern.compile("[\t, ]+");
        while (line != null) {
            if (line.length() == 0)
                continue;
            if (!Character.isDigit(line.charAt(0)))
                continue;

            // Extract the first 2 fields
            Scanner scanner = new Scanner(line);
            scanner.useLocale(Locale.US);
            scanner.useDelimiter(pattern);

            try {
                int molecules = scanner.nextInt();
                float frequency = scanner.nextFloat();

                // Check for duplicates
                for (float[] d : data) {
                    if (d[0] == molecules) {
                        error("Duplicate molecules field on line " + count);
                        return null;
                    }
                }

                data.add(new float[] { molecules, frequency });
            } catch (InputMismatchException e) {
                error("Incorrect fields on line " + count);
                return null;
            } catch (NoSuchElementException e) {
                error("Incorrect fields on line " + count);
                return null;
            } finally {
                scanner.close();
            }

            // Get the next line
            line = input.readLine();
            count++;
        }

        if (data.isEmpty()) {
            error("No data in file " + filename);
            return null;
        }

        // Create a contiguous histogram from zero
        int maxN = 0;
        for (float[] d : data) {
            if (maxN < d[0])
                maxN = (int) d[0];
        }

        float[][] hist = new float[2][maxN + 1];
        for (int n = 0; n <= maxN; n++) {
            hist[0][n] = n;
            for (float[] d : data) {
                if (n == d[0])
                    hist[1][n] = d[1];
            }
        }
        HistogramData histogramData = new HistogramData(hist, f, a, u);
        histogramData.filename = filename;
        return histogramData;
    } catch (IOException e) {
        IJ.error(TITLE, "Unable to read from file " + filename);
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
            // Ignore
        }
    }
    return null;
}

From source file:com.alibaba.dubbo.qos.textui.TTable.java

private String drawRow(int[] widthCacheArray, int rowIndex) {

    final StringBuilder rowSB = new StringBuilder();
    final Scanner[] scannerArray = new Scanner[getColumnCount()];
    try {//  ww  w  .  j  a va 2  s . c  om
        boolean hasNextLine;
        do {

            hasNextLine = false;
            final StringBuilder segmentSB = new StringBuilder();

            for (int colIndex = 0; colIndex < getColumnCount(); colIndex++) {

                final int width = widthCacheArray[colIndex];
                final boolean isFirstColOfRow = colIndex == 0;
                final boolean isLastColOfRow = colIndex == widthCacheArray.length - 1;

                final String borderChar;
                if (isFirstColOfRow && border.has(Border.BORDER_OUTER_LEFT)) {
                    borderChar = "|";
                } else if (!isFirstColOfRow && border.has(Border.BORDER_INNER_V)) {
                    borderChar = "|";
                } else {
                    borderChar = EMPTY;
                }

                if (null == scannerArray[colIndex]) {
                    scannerArray[colIndex] = new Scanner(
                            new StringReader(wrap(getData(rowIndex, columnDefineArray[colIndex]), width)));
                }
                final Scanner scanner = scannerArray[colIndex];

                final String data;
                if (scanner.hasNextLine()) {
                    data = scanner.nextLine();
                    hasNextLine = true;
                } else {
                    data = EMPTY;
                }

                if (width > 0) {
                    final ColumnDefine columnDefine = columnDefineArray[colIndex];
                    final String dataFormat = getDataFormat(columnDefine, width, data);
                    final String paddingChar = repeat(" ", padding);
                    segmentSB.append(format(borderChar + paddingChar + dataFormat + paddingChar, data));
                }

                if (isLastColOfRow) {
                    if (border.has(Border.BORDER_OUTER_RIGHT)) {
                        segmentSB.append("|");
                    }
                    segmentSB.append("\n");
                }

            }

            if (hasNextLine) {
                rowSB.append(segmentSB);
            }

        } while (hasNextLine);

        return rowSB.toString();
    } finally {
        for (Scanner scanner : scannerArray) {
            if (null != scanner) {
                scanner.close();
            }
        }
    }

}

From source file:dsfixgui.view.DSFGraphicsPane.java

public void setFPSFixKey(String newFPSFixKey) {
    try {/*from   ww w  .java2  s. c o  m*/
        String fpsFixCfg = readTextFile(ui.getDataFolder() + "\\" + FPS_FIX_FILES[1]);

        ui.printConsole(WRITING_FILE[0] + FPS_FIX_FILES[1] + "...");
        //The file to be written
        File writeFile = new File(ui.getDataFolder() + "\\" + FPS_FIX_FILES[1]);
        if (writeFile.exists()) {
            //If file exists, try to delete it
            if (!writeFile.delete()) {
                //If file cannot be deleted, throw OIOException
                throw new IIOException("Could not delete pre-existing file: " + FPS_FIX_FILES[1]);
            }
        }

        //Format each linebreak to be displayed correctly in a text file
        String formattedText = fpsFixCfg.replaceAll("\n", String.format("%n"));
        //Initialize BufferedWriter to write string to file
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(writeFile));
        //Write the file
        Scanner scanner = new Scanner(formattedText);
        int i = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("Key=")) {
                fileWriter.write("Key=" + newFPSFixKey);
                fileWriter.newLine();
                i++;
            } else if (line.length() > 1) {
                fileWriter.write(line);
                fileWriter.newLine();
                i++;
            } else if (i == 2 || i == 4) {
                fileWriter.newLine();
                i++;
            }
        }
        fileWriter.close();
        scanner.close();

        ui.printConsole(WRITING_FILE[1]);
    } catch (IOException ex) {
        ui.printConsole(FILES_EDITED_ERR[0] + FPS_FIX_FILES[1]);
        fpsFixKeyPicker.setDisable(true);
    }
}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Write document using StringHandle with metadata
 * @param client/*from www .  j a v  a 2s.  c  o  m*/
 * @param filename
 * @param uri
 * @param metadataHandle
 * @param type
 * @throws IOException
 */
public void writeDocumentUsingStringHandle(DatabaseClient client, String filename, String uri,
        DocumentMetadataHandle metadataHandle, String type) throws IOException {
    // acquire the content
    File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename);
    FileInputStream fis = new FileInputStream(file);
    Scanner scanner = new Scanner(fis).useDelimiter("\\Z");
    String readContent = scanner.next();
    fis.close();
    scanner.close();

    // create doc manager
    DocumentManager docMgr = null;
    docMgr = documentManagerSelector(client, docMgr, type);

    String docId = uri + filename;

    // create handle
    StringHandle contentHandle = new StringHandle();
    contentHandle.set(readContent);

    // write the doc
    docMgr.write(docId, metadataHandle, contentHandle);

    System.out.println("Write " + docId + " to the database");
}