Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:eu.digitisation.idiomaident.utils.ExtractTestSamples.java

private static void extractSamples(int numSamples, File inFolder, File outFile) {
    FileOutputStream fileOS = null;
    OutputStreamWriter outSWriter = null;

    try {//  w w w  .j a  v a2s .  com
        fileOS = new FileOutputStream(outFile);
        outSWriter = new OutputStreamWriter(fileOS, Charset.forName("UTF8"));

        int num = 0;
        while (num < numSamples) {
            String sample = nextSample(inFolder);

            if (sample == null)
                continue;

            outSWriter.write(sample);
            num++;
            System.out.println("Generating " + num + " of " + numSamples + " samples");
        }

        outSWriter.flush();

    } catch (IOException ex) {
        System.out.println(ex.toString());
    } finally {
        try {
            if (outSWriter != null) {
                outSWriter.close();
            }
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*w w  w .j a  v a 2 s.c  o  m*/
 *
 * @param filePath
 *            ?
 * @param fileContent
 *            ?
 * @return ?truefalse
 */
public static boolean writeFileByString(String filePath, String fileContent) {
    if (null == filePath || null == fileContent) {
        return false;
    }
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath));
        outputStreamWriter.write(fileContent, 0, fileContent.length());
        outputStreamWriter.flush();
        outputStreamWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
        XLog.d(CLASS_NAME, e.getMessage());
        return false;
    }
    return true;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

static Uri saveHistory(String history) {
    File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
    File historyRoot = new File(bsRoot, "History");
    if (!historyRoot.exists() && !historyRoot.mkdirs()) {
        Log.w(TAG, "Couldn't make dir " + historyRoot);
        return null;
    }//  w  w  w. j  a v  a 2s . c  om
    File historyFile = new File(historyRoot, "history-" + System.currentTimeMillis() + ".csv");
    OutputStreamWriter out = null;
    try {
        out = new OutputStreamWriter(new FileOutputStream(historyFile), Charset.forName("UTF-8"));
        out.write(history);
        return Uri.parse("file://" + historyFile.getAbsolutePath());
    } catch (IOException ioe) {
        Log.w(TAG, "Couldn't access file " + historyFile + " due to " + ioe);
        return null;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ioe) {
                // do nothing
            }
        }
    }
}

From source file:com.ieasy.basic.util.file.FileUtils.java

public static void WriteJSON(String outPath, String perfix, Object obj) {
    try {//from  w ww .ja  v a 2  s  .co  m
        String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss");
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outPath), "UTF-8");
        out.write((null != perfix ? perfix : "") + json);
        out.flush();
        out.close();
    } catch (IOException e) {
        logger.error("?JSON" + e.getMessage());
    }
    logger.debug("?JSON-->" + outPath);
}

From source file:de.laures.cewolf.util.Renderer.java

/**
 * Handles rendering a chart as a SVG. Currently this method is synchronized
 * because of concurrency issues with JFreeChart.
 *
 * @param  baos/*from www.ja v a 2s.  co m*/
 * @param  chart
 * @param  width
 * @param  height
 * @throws IOException
 */
private static synchronized void handleSVG(ByteArrayOutputStream baos, JFreeChart chart, int width, int height)
        throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(baos, "UTF-8");
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument("cewolf-svg", "svg", null);
    SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
    ctx.setComment("Generated by Cewolf using JFreeChart and Apache Batik SVG Generator");
    SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), null);
    svgGenerator.stream(writer, false);
    writer.close();
}

From source file:net.iharding.utils.FileUtils.java

/**
 * /*w w  w  .j av a 2s.  c  om*/
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            //            FileWriter fileWriter = new FileWriter(filePath, true);
            //            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            //???
            OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.close();
            fileWriter.close();
        } else {
            log.info("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dm.estore.common.utils.FileUtils.java

/**
 *
 * @param inputStream Input stream to read data from.
 * @param outputName Target file name to write data in.
 *///from   w ww. j a  v a  2s .  c o m
public static void saveFile(InputStream inputStream, String outputName) {
    OutputStreamWriter out = null;
    try {
        out = new OutputStreamWriter(new FileOutputStream(outputName), DEFAULT_ENCODING);
        final BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
        String inputLine = inputReader.readLine();
        while (inputLine != null) {
            out.append(inputLine);
            inputLine = inputReader.readLine();
            if (inputLine != null) {
                out.append(NEW_LINE_SEP);
            }
        }
        inputReader.close();
    } catch (Exception e) {
        LOG.error("Unable to write file: " + outputName, e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                LOG.trace("Unable to close file: " + outputName, e);
            }
        }
    }
}

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

public static void writeStr(final String str, final String filePath) throws Exception {

    OutputStreamWriter out = null;

    try {// w  ww  .  j a va  2 s . c o  m

        out = new OutputStreamWriter(openOrCreateFileWithPathForWrite(filePath), SSEncodingU.utf8.toString());

        out.write(str);

    } finally {

        if (out != null) {
            out.close();
        }
    }
}

From source file:com.ms.commons.test.treedb.Objects2XmlFileUtil.java

public static void write(String testMethodName, BuiltInCacheKey prePareOrResult, List<?>... prepareData) {

    OutputStreamWriter writer = null;
    File file = getFile(testMethodName, prePareOrResult);
    try {/* w  w w.j a  v  a 2s.com*/
        writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        XStream xStream = new XStream();
        StringBuilder root = new StringBuilder();

        root.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        root.append("<").append(ROOT).append(">");

        for (List<?> objectList : prepareData) {
            if (objectList == null || objectList.size() <= 0)
                continue;

            StringBuilder objectStr = objectList2XmlString(xStream, objectList);

            root.append(objectStr);
        }

        root.append("</").append(ROOT).append(">");

        writer.write(prettyPrint(root.toString()));
        writer.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("write " + file.getAbsolutePath() + " failed", e);
    } catch (IOException e) {
        throw new RuntimeException("write " + file.getAbsolutePath() + " failed", e);
    } finally {
        close(writer);
    }
}

From source file:org.opendatakit.survey.android.provider.SubmissionProvider.java

/**
 * This method actually writes the JSON appName-relative manifest to disk.
 *
 * @param payload/*from  www .  j a v a 2 s  .c  o m*/
 * @param path
 * @return
 */
private static boolean exportFile(String payload, File outputFilePath, WebLogger log) {
    // write xml file
    FileOutputStream os = null;
    OutputStreamWriter osw = null;
    try {
        os = new FileOutputStream(outputFilePath, false);
        osw = new OutputStreamWriter(os, CharEncoding.UTF_8);
        osw.write(payload);
        osw.flush();
        osw.close();
        return true;

    } catch (IOException e) {
        log.e(t, "Error writing file");
        e.printStackTrace();
        try {
            osw.close();
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}