Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:com.sforce.cd.apexUnit.fileReader.ApexManifestFileReaderTest.java

@BeforeTest
public void setup() throws IOException, URISyntaxException {
    String soql = "";
    // writing to a file in the current working directory
    LOG.info("Working directory : " + workingDir);
    PartnerConnection connection = ConnectionHandler.getConnectionHandlerInstance().getConnection();
    File dir = new File(workingDir);
    dir.mkdirs();/*from  w  ww .  j a va 2s . c  o m*/
    File file = new File(dir, fileName);
    soql = "SELECT Id , Name FROM ApexClass LIMIT 10";
    QueryResult queryResult = null;
    try {
        queryResult = connection.query(soql);
        LOG.info("soql: " + soql);
    } catch (ConnectionException e) {
        ApexUnitUtils.shutDownWithDebugLog(e, "Connection Exception encountered when trying to query : " + soql
                + " \n The connection exception description says : " + e.getMessage());
    }
    if (queryResult != null) {
        testClassesAsString = fetchApexClassesAsString(queryResult);
        LOG.info("testClassesAsString: " + testClassesAsString);
    }

    try {

        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(testClassesAsString);
        bw.close();
        String cvsSplitBy = ",";
        String[] manifestFilesAsArray = manifestFiles.split(cvsSplitBy);
        for (String manifestFile : manifestFilesAsArray) {
            LOG.info("Creating Manifest file : " + manifestFile);
            File tmpFile = new File(dir, manifestFile);
            FileWriter fileWriter = new FileWriter(tmpFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(testClassesAsString);
            bufferedWriter.close();
        }
    } catch (IOException e) {
        ApexUnitUtils.shutDownWithDebugLog(e, "IO Exception caught \n");
    }

}

From source file:at.tugraz.sss.serv.util.SSFileU.java

public static void appendTextToFile(String filePath, String text) throws SSErr {

    FileWriter fileWriter = null;
    BufferedWriter bufferWritter = null;

    try {/*from  w  w w  .ja  v a2 s  .  c om*/
        fileWriter = new FileWriter(filePath, true);
        bufferWritter = new BufferedWriter(fileWriter);

        bufferWritter.write(text);

    } catch (IOException error) {
        SSServErrReg.regErrThrow(error);
    } finally {

        if (bufferWritter != null) {
            try {
                bufferWritter.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }

        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }
    }
}

From source file:com.o2d.pkayjava.editor.CustomExceptionHandler.java

private void writeToFile(String stacktrace, String filename) {
    try {/* w ww . j av a 2s  .c o m*/
        //String localPath = DataManager.getMyDocumentsLocation();
        String localPath = "";//DataManager.getInstance().getRootPath();
        System.out.println(localPath + File.separator + filename);
        BufferedWriter bos = new BufferedWriter(new FileWriter(localPath + File.separator + filename));
        bos.write(stacktrace);
        bos.flush();
        bos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.tencent.wetest.common.util.ReportUtil.java

public static void updateRecord(File f, String name, String record) {

    path = WTApplication.getContext().getFilesDir().getPath();

    List<String> content = new ArrayList<String>();

    if (f.exists() && f.isFile()) {

        try {//  w w w. ja  v  a 2  s  .  c o  m

            String br = "";
            boolean hasRecord = false;
            BufferedReader indexreader = new BufferedReader(new FileReader(f));

            while ((br = indexreader.readLine()) != null) {
                if (br.split("/")[0].equals(name)) {

                    content.add(record);
                    hasRecord = true;
                } else {
                    content.add(br);

                }
            }

            if (!hasRecord)
                content.add(record);

            indexreader.close();

            BufferedWriter indexwriter = new BufferedWriter(new FileWriter(f, false));

            if (content.size() == 0) {

                indexwriter.write(record);

            } else {
                int i = 0;
                for (String temp : content) {

                    if (i == content.size() - 1) {
                        indexwriter.write(temp);
                    } else {
                        indexwriter.write(temp + "\t\n");
                    }
                    i++;
                }
            }

            indexwriter.flush();
            indexwriter.close();

        } catch (Exception e) {

            Logger.error("updateRecord exception : " + e.toString());
            e.printStackTrace();
        }

    }
}

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

void unregisterThread() {
    try {//from ww w.  j a  v a  2 s  .c  om
        BufferedWriter bf = logger.get();
        if (bf != original) {
            bf.close();
            logger.remove();
        }
    } catch (IOException e) {
        // A PrintStream is supposed to never throw IOException
        jqmlogger.warn("could not close log file", e);
    }
}

From source file:it.wingstech.csslesser.LessEngine.java

public void compile(File input, File output) throws LessException, IOException {
    try {//from  w  w  w  .  j ava 2s  .co m
        String content = compile(input);
        if (!output.exists()) {
            output.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        bw.write(content);
        bw.close();
    } catch (Exception e) {
        throw parseLessException(e);
    }
}

From source file:gov.jgi.meta.MetaUtils.java

/**
 * given a map of sequences indexed by sequence id, write out a fasta file to local file system.
 * files are created with rwx bits set to 777.
 *
 * @param seqList is the list of sequences to create the database with
 * @param tmpFileName the file name/path to create
 * @return the full path of the location of the database
 * @throws IOException if error occures in file creation
 *///from w ww . j  ava  2 s .co  m
public static String sequenceToLocalFile(Map<String, String> seqList, String tmpFileName) throws IOException {
    BufferedWriter out;
    File seqFile = null;

    /*
     * open temp file
     */
    seqFile = new File(tmpFileName);
    out = new BufferedWriter(new FileWriter(seqFile.getPath()));

    /*
     * write out the sequences to file
     */
    for (String key : seqList.keySet()) {
        assert (seqList.get(key) != null);
        out.write(">" + key + "\n");
        out.write(seqList.get(key) + "\n");
    }

    /*
     * close temp file
     */
    out.close();

    if (!(seqFile.setExecutable(true, false) && seqFile.setReadable(true, false)
            && seqFile.setWritable(true, false))) {
        throw new IOException("unable to set RWX bits to 777");
    }

    return (seqFile.getPath());
}

From source file:eu.crisis_economics.abm.simulation.injection.TestFromFileNumercialModelParameter.java

/**
  * Test whether an instance of {@link FromFileTimeseriesParameter} correctly
  * reads, and correctly reports, a timeseries from a datafile. This unit test 
  * operates as follows:<br><br>
  * // w ww . j a  v a2  s . c o  m
  * {@code (a)}
  *    A temporary file {@code F}, named {@code "./paramter-test.dat"} is created 
  *    in the local directory;<br>
  * {@code (b)}
  *    {@code F} is populated with a short discrete subsequence from the expression
  *    {@code f(T) = T**2};<br>
  * {@code (c)}
  *    {@code F} is parsed by an instance of {@link FromFileTimeseriesParameter};<br>
  * {@code (d)}
  *    An {@link EmptySimulation} is run for {@code 10} cycles. At each cycle,
  *    it is asserted that the {@link ModelParameter} yields the same expression
  *    {@code f(T)} as is indicated by {@code F}.<br><br>
  *    
  * For convenience {@code F} is not deleted at the end of the above session.
  */
@Test
public void testReadModelParameterTimeSeriesFromFile() {
    final double[] expectedX = new double[] { 2., 3., 4., 5., 6., },
            expectedY = new double[] { 4., 9., 16., 25., 36., };
    final String fileName = "./paramter-test.dat";
    try {
        final File file = new File(fileName);
        if (!file.exists())
            file.createNewFile();
        final FileWriter stream = new FileWriter(file.getAbsoluteFile());
        final BufferedWriter writer = new BufferedWriter(stream);
        for (int i = 0; i < expectedX.length; ++i)
            writer.write(expectedX[i] + "\t" + expectedY[i] + "\n");
        writer.close();
        stream.close();
    } catch (final IOException e) {
        Assert.fail();
    }
    ModelParameter<Double> parameter = null;
    try {
        parameter = new FromFileTimeseriesParameter(fileName, "[name]", new SplineInterpolator());
    } catch (final IOException e) {
        Assert.fail();
    }
    Simulation.repeat(this, "sample", CustomSimulationCycleOrdering.create(0.), parameter, expectedX,
            expectedY);
    while (Simulation.getTime() < 10.0)
        state.schedule.step(state);
}

From source file:com.att.voice.AmazonEchoApi.java

public void addItemId(String itemId) throws IOException {
    File file = new File("Items.txt");
    try {//from   w  w w. j  a v  a 2s .c  o m
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWritter = new FileWriter(file.getName(), true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write(itemId + "\n");
        bufferWritter.close();
    } catch (Exception e) {
    }
}