Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out) 

Source Link

Document

Creates an OutputStreamWriter that uses the default character encoding.

Usage

From source file:mainserver.Server.java

public static void sendMessage(String text, Socket clientSocket) {
    try {//from w ww .j a v  a2  s . com
        BufferedWriter outputToServer = new BufferedWriter(
                new OutputStreamWriter(clientSocket.getOutputStream()));
        outputToServer.write(text + "\r\n");
        outputToServer.flush();

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

}

From source file:es.ua.dlsi.patch.translation.LocalApertiumTranslator.java

public Set<String> getTranslation(final String input) {

    Set<String> output = new HashSet<>();
    String finalline = "";
    // pull from the map if already there

    try {//  w w  w .j av a  2 s .c om
        String[] command = { "apertium", "-u", langCmd };
        ProcessBuilder probuilder = new ProcessBuilder(command);

        Process process = probuilder.start();
        OutputStream stdin = process.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
        writer.write(input);
        writer.newLine();
        writer.flush();
        writer.close();

        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            finalline += line;
        }
        br.close();

    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    }
    output.add(finalline);
    return output;
}

From source file:com.sciaps.utils.ImportExportSpectrumCSV.java

public void exportSpectrumFile(File saveFile, PiecewiseSpectrum spectrum) throws IOException {
    if (spectrum == null || saveFile == null) {
        logger_.warn("", "will not save spectrum csv file");
        return;/*  w  w  w .j  a va  2  s . c om*/
    }

    final UnivariateFunction intensity = spectrum.getIntensityFunction();

    BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveFile)));
    try {
        bout.append("wavelength, intensity");
        bout.newLine();
        final DoubleRange range = spectrum.getValidRange();

        for (double x = range.getMinimumDouble(); x <= range.getMaximumDouble(); x += 1.0
                / EXPORT_SAMPLE_RATE) {
            double y = intensity.value(x);
            if (Double.isNaN(y)) {
                y = 0;
            }
            bout.append(Double.toString(x));
            bout.append(", ");
            bout.append(Double.toString(y));
            bout.newLine();
        }
    } finally {
        bout.close();
    }

    logger_.info("saved spectrum csv file to " + saveFile.getAbsolutePath());
}

From source file:com.smash.revolance.ui.materials.TemplateHelper.java

public static String processTemplate(String templateName, Map<String, Object> variables)
        throws TemplateException, IOException {
    // load template
    Template template = null;// w ww. j  a  v  a  2s . c o  m
    InputStreamReader stream = null;
    try {
        InputStream content = TemplateHelper.class.getClassLoader().getResourceAsStream(templateName);
        if (content == null) {
            System.err.println("Unable to find template: " + templateName);
            return "Unable to find template: " + templateName;
        } else {
            stream = new InputStreamReader(content);
            template = new Template("template", stream, null);
        }
    } catch (IOException e) {
        System.err.println("Unable to find template: " + templateName);
        return "Unable to find template: " + templateName;
    } finally {
        IOUtils.closeQuietly(stream);
    }
    // process template
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (template != null) {
        // Fill the template with parameter values
        Writer out = new OutputStreamWriter(baos);
        template.process(variables, out);
        out.flush();
    }
    return baos.toString();
}

From source file:coolmap.application.io.internal.coolmapobject.InternalCoolMapObjectIO.java

/**
 * save the coolmap property to a JSON file - if exception occurs then
 * project can't be saved correctly/*from   w  w w .  ja v a2 s . c  o  m*/
 *
 * @param object
 * @param objectFolder
 * @throws Exception
 */
private static void saveProperties(CoolMapObject object, TFile objectFolder) throws Exception {
    TFile propertyFile = new TFile(objectFolder.getAbsolutePath() + File.separator + IOTerm.FILE_PROPERTY);
    TFile stateFolder = new TFile(objectFolder.getAbsolutePath() + File.separator + IOTerm.DIR_STATE);

    //
    //Save coolMapBasic property
    BufferedWriter propertyWriter = new BufferedWriter(
            new OutputStreamWriter(new TFileOutputStream(propertyFile)));

    JSONObject property = new JSONObject();
    property.put(IOTerm.ATTR_ID, object.getID());
    property.put(IOTerm.ATTR_NAME, object.getName());
    CoolMapView view = object.getCoolMapView();
    property.put(IOTerm.ATTR_VIEW_ZOOM, new float[] { view.getZoomX(), view.getZoomY() });
    Point mapAnchor = object.getCoolMapView().getMapAnchor();
    property.put(IOTerm.ATTR_VIEW_ANCHOR, new int[] { mapAnchor.x, mapAnchor.y });

    CoolMapView v = object.getCoolMapView();
    property.put(IOTerm.ATTR_VIEW_MAXIMIZED, v.isMaximized());
    property.put(IOTerm.ATTR_VIEW_MINIMIZED, v.isMinimized());

    Rectangle b = v.getBounds();
    property.put(IOTerm.ATTR_VIEW_BOUNDS, new int[] { b.x, b.y, b.width, b.height });

    ArrayList<String> linkedMxIDs = new ArrayList<String>();
    List<CMatrix> linkedMxs = object.getBaseCMatrices();
    for (CMatrix mx : linkedMxs) {
        linkedMxIDs.add(mx.getID());
    }

    property.put(IOTerm.ATTR_VIEW_MATRICES, linkedMxIDs);

    if (object.getAggregator() != null) {
        property.put(IOTerm.ATTR_VIEW_AGGREGATOR_CLASS, object.getAggregator().getClass().getName());
    }

    if (object.getViewRenderer() != null) {
        property.put(IOTerm.ATTR_VIEW_RENDERER_CLASS, object.getViewRenderer().getClass().getName());
    }

    if (object.getSnippetConverter() != null) {
        property.put(IOTerm.ATTR_VIEW_SNIPPETCONVERTER_CLASS,
                object.getSnippetConverter().getClass().getName());
    }

    //Save the side panels used in CoolMapView
    boolean rowPanelVisible = object.getCoolMapView().isRowPanelsVisible();
    boolean columnPanelVisible = object.getCoolMapView().isColumnPanelsVisible();

    JSONObject rowPanelConfig = new JSONObject();
    property.put(IOTerm.ATTR_VIEW_PANEL_ROW, rowPanelConfig);

    if (rowPanelVisible) {
        rowPanelConfig.put(IOTerm.ATTR_VIEW_PANEL_CONTAINER_VISIBLE, true);
    }
    //figure out which panels are visible
    List<RowMap> rowMaps = object.getCoolMapView().getRowMaps();
    ArrayList<JSONObject> rowMapsList = new ArrayList<>(rowMaps.size());

    for (RowMap map : rowMaps) {
        JSONObject rowMapEntry = new JSONObject();
        rowMapsList.add(rowMapEntry);
        rowMapEntry.put(IOTerm.ATTR_CLASS, map.getClass().getName());

        //config
        JSONObject config = map.getCurrentState();
        if (config != null) {
            rowMapEntry.put(IOTerm.ATTR_CONFIG, config);
        }
    }

    rowPanelConfig.put(IOTerm.ATTR_VIEW_PANEL, rowMapsList);

    ///////////////////////////////////////////////
    //columnMaps
    JSONObject columnPanelConfig = new JSONObject();
    property.put(IOTerm.ATTR_VIEW_PANEL_COLUMN, columnPanelConfig);
    if (columnPanelVisible) {
        columnPanelConfig.put(IOTerm.ATTR_VIEW_PANEL_CONTAINER_VISIBLE, true);

    }

    List<ColumnMap> columnMaps = object.getCoolMapView().getColumnMaps();
    ArrayList<JSONObject> columnMapsList = new ArrayList<>(columnMaps.size());

    //
    for (ColumnMap map : columnMaps) {
        JSONObject colMapEntry = new JSONObject();
        columnMapsList.add(colMapEntry);
        colMapEntry.put(IOTerm.ATTR_CLASS, map.getClass().getName());

        //config
        JSONObject config = map.getCurrentState();
        if (config != null) {
            colMapEntry.put(IOTerm.ATTR_CONFIG, config);
        }
    }

    columnPanelConfig.put(IOTerm.ATTR_VIEW_PANEL, columnMapsList);

    //figure out which panels are visible
    propertyWriter.write(property.toString());
    propertyWriter.flush();
    propertyWriter.close();

    try {
        //Save aggregator property JSON
        if (object.getAggregator() != null) {
            JSONObject aggregatorProperty = object.getAggregator().getCurrentState();
            if (aggregatorProperty != null) {
                BufferedWriter aggregatorWriter = new BufferedWriter(
                        new OutputStreamWriter(new TFileOutputStream(objectFolder.getAbsolutePath()
                                + File.separator + IOTerm.FILE_PROPERTY_RENDERER)));

                aggregatorWriter.write(aggregatorProperty.toString());

                aggregatorWriter.flush();
                aggregatorWriter.close();
            }
        }
    } catch (Exception e) {
        //can still continue, just the aggreat
        System.err.println("Aggregator state saving error");
    }

    //Save renderer property JSON
    try {
        if (object.getViewRenderer() != null) {
            JSONObject rendererProperty = object.getViewRenderer().getCurrentState();
            if (rendererProperty != null) {

                BufferedWriter viewRendererWriter = new BufferedWriter(
                        new OutputStreamWriter(new TFileOutputStream(objectFolder.getAbsolutePath()
                                + File.separator + IOTerm.FILE_PROPERTY_RENDERER)));

                viewRendererWriter.write(rendererProperty.toString());

                viewRendererWriter.flush();
                viewRendererWriter.close();
            }
        }
    } catch (Exception e) {
        System.err.println("Renderer state saving error");
    }

    //Save snippet property JSON
    try {
        if (object.getSnippetConverter() != null) {
            JSONObject snippetProperty = object.getAggregator().getCurrentState();
            if (snippetProperty != null) {
                BufferedWriter snippetWriter = new BufferedWriter(new OutputStreamWriter(new TFileOutputStream(
                        objectFolder.getAbsolutePath() + File.separator + IOTerm.FILE_PROPERTY_RENDERER)));

                snippetWriter.write(snippetProperty.toString());

                snippetWriter.flush();
                snippetWriter.close();
            }

        }
    } catch (Exception e) {
        System.out.println("Snippet state saving erorr");
    }

}

From source file:org.iwethey.forums.web.json.FastJsonView.java

public void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws ServletException {
    try {/*from  w w w  . j  a v  a2 s .  co m*/
        OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());

        Object data = model.get("json");

        Object serTmp = model.get("serializer");
        JacksonSerializer ser = null;
        if (serTmp != null && JacksonSerializer.class.isAssignableFrom(serTmp.getClass())) {
            ser = (JacksonSerializer) serTmp;
        } else {
            ser = new JacksonSerializer();
        }

        if (data != null) {
            ser.serialize(data, out);
        }

        out.flush();
    } catch (IOException ie) {
        throw new ServletException(ie);
    } catch (JsonException e) {
        throw new ServletException(e);
    }
}

From source file:matrix.CreateTextMatrix.java

public void textMatrix()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

    CosSim cossim = new CosSim();
    JSONParser jParser = new JSONParser();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweets.json"), "ISO-8859-9"));
    JSONArray a = (JSONArray) jParser.parse(in);
    File fout = new File("/Users/nSabri/Desktop/tweetMatris.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    for (int i = 0; i < 1000; i++) {

        for (int j = 0; j < 1000; j++) {

            JSONObject tweet1 = (JSONObject) a.get(i);
            JSONObject tweet2 = (JSONObject) a.get(j);
            String tweetText1 = tweet1.get("tweets").toString();
            String tweetText2 = tweet2.get("tweets").toString();

            double CosSimValue = cossim.Cosine_Similarity_Score(tweetText1, tweetText2);
            CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
            bw.write(Double.toString(CosSimValue) + ", ");

        }//from ww w .  ja  va2s .  c  o m
        bw.newLine();
    }
    bw.close();

}

From source file:matrix.CreateUrlMatrix.java

public void urlMatrix()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

    CosSim cossim = new CosSim();
    JSONParser jParser = new JSONParser();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweetsUrls.json"), "ISO-8859-9"));
    JSONArray a = (JSONArray) jParser.parse(in);
    File fout = new File("/Users/nSabri/Desktop/urlMatris.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    for (int i = 0; i < a.size(); i++) {

        for (int j = 0; j < a.size(); j++) {

            JSONObject tweet1 = (JSONObject) a.get(i);
            JSONObject tweet2 = (JSONObject) a.get(j);
            String tweetUrl1 = tweet1.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl1);
            String tweetUrl2 = tweet2.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl2);

            double CosSimValue = cossim.Cosine_Similarity_Score(tweetUrl1, tweetUrl2);
            CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
            bw.write(Double.toString(CosSimValue) + ", ");

        }// w ww .j  av a2  s . c om
        bw.newLine();
    }
    bw.close();

}

From source file:com.cloudera.sqoop.io.SplittableBufferedWriter.java

public SplittableBufferedWriter(final SplittingOutputStream splitOutputStream) {
    super(new OutputStreamWriter(splitOutputStream));

    this.splitOutputStream = splitOutputStream;
    this.alwaysFlush = false;
}

From source file:info.archinnov.achilles.test.integration.utils.CassandraLogAsserter.java

public void prepareLogLevel() {
    logStream = new ByteArrayOutputStream();
    storageProxyLogger.setLevel(Level.TRACE);
    writerAppender = new WriterAppender();
    writerAppender.setWriter(new OutputStreamWriter(logStream));
    writerAppender.setLayout(new PatternLayout("%-5p [%d{ABSOLUTE}][%x] %c@:%M %m %n"));
    storageProxyLogger.addAppender(writerAppender);

}