Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

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

Prototype

public void write(String str, int off, int len) throws IOException 

Source Link

Document

Writes a portion of a string.

Usage

From source file:im.vector.util.BugReporter.java

/**
 * Read the file content as String//from   ww  w.  ja  v a  2 s .c  om
 *
 * @param fin the input file
 * @return the file content as String
 */
private static String convertStreamToString(File fin) {
    Reader reader = null;

    try {
        Writer writer = new StringWriter();
        InputStream inputStream = new FileInputStream(fin);
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;

            char[] buffer = new char[2048];
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
            }
        }
        return writer.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + e.getMessage());
    } catch (OutOfMemoryError oom) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + oom.getMessage());
    } finally {
        try {
            if (null != reader) {
                reader.close();
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
        }
    }

    return "";
}

From source file:hudson.Util.java

public static void copyStream(Reader in, Writer out) throws IOException {
    char[] buf = new char[8192];
    int len;/*w  w  w.j  a  va  2s.co  m*/
    while ((len = in.read(buf)) > 0)
        out.write(buf, 0, len);
}

From source file:dk.ange.octave.util.TeeWriter.java

@Override
public void write(final char[] cbuf, final int off, final int len) throws IOException {
    IOException ioe = null;/*w  w  w . jav a2  s.  c  om*/
    for (final Writer writer : writers) {
        try {
            writer.write(cbuf, off, len);
        } catch (final IOException e) {
            log.debug("Exception during write()", e);
            ioe = e;
        }
    }
    if (ioe != null) {
        throw ioe;
    }
}

From source file:it.govpay.web.utils.Utils.java

public static void copy2(Reader in, Writer out) throws IOException {

    // do not allow other threads to read from the
    // input or write to the output while copying is
    // taking place

    synchronized (in) {
        synchronized (out) {

            char[] buffer = new char[256];
            while (true) {
                int bytesRead = in.read(buffer);
                if (bytesRead == -1)
                    break;
                out.write(buffer, 0, bytesRead);
            }/* w  w  w .j ava 2  s. c o  m*/
        }
    }
}

From source file:mupomat.utility.LongLatService.java

public void getLongitudeLatitude(String address) {
    try {/*from   w  w  w  .ja v a 2 s.co m*/
        StringBuilder urlBuilder = new StringBuilder(GEOCODE_REQUEST_URL);
        if (StringUtils.isNotBlank(address)) {
            urlBuilder.append("&address=").append(URLEncoder.encode(address, "UTF-8"));
        }

        final GetMethod getMethod = new GetMethod(urlBuilder.toString());
        try {
            httpClient.executeMethod(getMethod);
            Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(),
                    getMethod.getResponseCharSet());

            int data = reader.read();
            char[] buffer = new char[1024];
            Writer writer = new StringWriter();
            while ((data = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, data);
            }

            String result = writer.toString();
            System.out.println(result.toString());

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader("<" + writer.toString().trim()));
            Document doc = db.parse(is);

            strLatitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lat/text()");
            System.out.println("Latitude:" + strLatitude);

            strLongtitude = getXpathValue(doc, "//GeocodeResponse/result/geometry/location/lng/text()");
            System.out.println("Longitude:" + strLongtitude);

        } finally {
            getMethod.releaseConnection();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}