Example usage for java.io BufferedWriter BufferedWriter

List of usage examples for java.io BufferedWriter BufferedWriter

Introduction

In this page you can find the example usage for java.io BufferedWriter BufferedWriter.

Prototype

public BufferedWriter(Writer out) 

Source Link

Document

Creates a buffered character-output stream that uses a default-sized output buffer.

Usage

From source file:org.jvnet.hudson.generators.HudsonConfigGenerator.java

@Override
public String generate() {
    String configBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, HUDSON_CONFIG_TEMPLATE,
            getModel());/*from www  . j a  v a2s  .c  o m*/

    BufferedWriter bufferedWriter = null;
    try {
        File file = new File(outputDirectory);
        //noinspection ResultOfMethodCallIgnored
        file.mkdirs();
        file = new File(outputDirectory + File.separatorChar + CONFIG_FILE_NAME);

        bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write(configBody);
        bufferedWriter.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bufferedWriter != null) {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return configBody;
}

From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsLocksContext.java

LfsLocksContext(LfsGson gson, final HttpServletRequest req, final HttpServletResponse res) {
    this.path = req.getPathInfo().startsWith("/") ? req.getPathInfo() : "/" + req.getPathInfo();
    this.req = req;
    this.res = res;
    this.writer = Suppliers.memoize(new Supplier<Writer>() {
        @Override/*from ww  w. ja  v  a  2  s . c o  m*/
        public Writer get() {
            try {
                return new BufferedWriter(new OutputStreamWriter(res.getOutputStream(), UTF_8));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    this.reader = Suppliers.memoize(new Supplier<Reader>() {
        @Override
        public Reader get() {
            try {
                return new BufferedReader(new InputStreamReader(req.getInputStream(), UTF_8));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    this.gson = gson;
    setLfsResponseType();
}

From source file:eionet.gdem.dcm.conf.DcmProperties.java

public void setDbParams(String url, String user, String psw) throws DCMException {

    String filePath = Properties.appHome + File.separatorChar + "gdem.properties";

    try {//from w  w w  . j  ava 2 s . co  m

        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line = null;
        StringBuffer st = new StringBuffer();

        while ((line = reader.readLine()) != null) {
            // process the line
            line = findSetProp(line, "db.url", url);
            line = findSetProp(line, "db.user", user);
            line = findSetProp(line, "db.pwd", psw);
            st.append(line);
            st.append("\n");
        }

        BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
        out.write(st.toString());
        out.close();
    } catch (IOException e) {
        LOGGER.error("Saving database parameters failed!", e);
        e.printStackTrace();
        throw new DCMException(BusinessConstants.EXCEPTION_PARAM_DB_FAILED);
    }
}

From source file:cc.redpen.formatter.JSONFormatter.java

@Override
public String format(Document document, List<ValidationError> errors) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedWriter writer = new BufferedWriter(new PrintWriter(baos));
    try {/*www .  j a  v  a 2 s. c o  m*/
        writer.write(asJSON(document, errors).toString());
        writer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}

From source file:au.org.ala.delta.intkey.model.StartupUtils.java

/**
 * Save a copy of a dataset that was opened from a remote location
 * //from   w ww .  j a  v a  2 s .  c  o  m
 * @param context
 *            Intkey context
 * @param saveDir
 *            Directory in which to save a copy of the dataset
 * @return A copy of the JNLP-style dataset startup file to use to open the
 *         saved copy of the dataset.
 * @throws IOException
 *             If saving to disk failed.
 */
public static File saveRemoteDataset(IntkeyContext context, File saveDir) throws IOException {
    StartupFileData startupFileData = context.getStartupFileData();
    File datasetZip = startupFileData.getDataFileLocalCopy();

    // Copy the zipped dataset as downloaded from the web
    // FileUtils.copyFileToDirectory(datasetZip, saveDir);

    // Copy the zipped dataset as downloaded from the web
    // Use utility method to avoid overwriting existing files with the same
    // name
    File copyZipFile = Utils.getSaveFileForDirectory(saveDir, datasetZip.getName());
    FileUtils.copyFile(datasetZip, copyZipFile);

    // Write a new .ink file
    // Use utility method to avoid overwriting existing files with the same
    // name
    File newInkFile = Utils.getSaveFileForDirectory(saveDir,
            FilenameUtils.getName(startupFileData.getInkFileLocation().getFile()));

    FileWriter fw = new FileWriter(newInkFile);
    BufferedWriter bufFW = new BufferedWriter(fw);

    bufFW.append(INIT_FILE_INK_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(newInkFile.toURI().toURL().toString());
    bufFW.append("\n");

    bufFW.append(INIT_FILE_DATA_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(copyZipFile.toURI().toURL().toString());
    bufFW.append("\n");

    bufFW.append(INIT_FILE_INITIALIZATION_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(startupFileData.getInitializationFileLocation());
    bufFW.append("\n");

    String imagePath = startupFileData.getImagePath();
    if (imagePath != null) {
        bufFW.append(INIT_FILE_IMAGE_PATH_KEYWORD);
        bufFW.append("=");
        bufFW.append(imagePath);
        bufFW.append("\n");
    }

    String infoPath = startupFileData.getInfoPath();
    if (infoPath != null) {
        bufFW.append(INIT_FILE_INFO_PATH_KEYWORD);
        bufFW.append("=");
        bufFW.append(infoPath);
        bufFW.append("\n");
    }

    bufFW.flush();
    bufFW.close();

    return newInkFile;
}

From source file:fr.itinerennes.bundler.tasks.framework.AbstractCountedCsvTask.java

@PostExec
public void prependLineCount() throws IOException {

    LOGGER.debug("Inserting line count at file head: {}", lineCount);

    final File output = getOutputFile();
    final File source = File.createTempFile("itr-", output.getName(), output.getParentFile());
    source.delete();//from  w  w  w.  j  a  va 2s  .c  o  m
    FileUtils.moveFile(output, source);

    InputStream from = null;
    BufferedWriter to = null;
    try {
        from = new FileInputStream(source);
        to = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output), CHARSET));
        to.write(String.valueOf(lineCount));
        to.newLine();
        final LineIterator i = IOUtils.lineIterator(from, CHARSET.name());
        while (i.hasNext()) {
            to.write(i.next());
            to.newLine();
        }
    } finally {
        IOUtils.closeQuietly(from);
        IOUtils.closeQuietly(to);
        FileUtils.deleteQuietly(source);
    }
}

From source file:com.packtpub.mahout.cookbook.chapter01.App.java

private static void CreateCsvRatingsFile() throws FileNotFoundException, IOException {

    BufferedReader br = new BufferedReader(new FileReader(inputFile));
    BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));

    String line = null;//from  w  w w. j  a v a 2  s  . c  o  m
    String line2write = null;
    String[] temp;

    int i = 0;
    while ((line = br.readLine()) != null && i < 1000) {
        i++;
        temp = line.split("::");
        line2write = temp[0] + "," + temp[1];
        bw.write(line2write);
        bw.newLine();
        bw.flush();

    }
    br.close();
    bw.close();
}

From source file:de.dakror.scpuller.SCPuller.java

public static void saveDownloadedSongs() {
    File f = new File(System.getProperty("user.home") + "/.dakror/SCPuller/downloaded.txt");
    try {//from w  ww .  j a v a 2s .c om
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        for (int i : downloadedSongs)
            bw.write(i + "\r\n");

        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.enioka.jqm.tools.MulticastPrintStream.java

MulticastPrintStream(OutputStream out, String rootLogDir) {
    super(out);//from  ww w  .ja va  2s .c  o  m
    this.original = new BufferedWriter(new OutputStreamWriter(out));
    this.rootLogDir = rootLogDir;

    File d = new File(this.rootLogDir);
    if (!d.isDirectory() && !d.mkdir()) {
        throw new JqmInitError("could not create log dir " + this.rootLogDir);
    }
}

From source file:files.FileUtils.java

public static boolean WriteDatFile(String pathFileText, String fileContent) {
    try {//from   w ww  .  j  a  v  a2s.c o m
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        if (!IsNullOrEmpty(fileContent)) {
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), true);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(fileContent);
            bufferWritter.close();

            return true;
        }
    } catch (Exception ex) {
        return false;
    }
    return false;
}