Example usage for java.io OutputStreamWriter write

List of usage examples for java.io OutputStreamWriter write

Introduction

In this page you can find the example usage for java.io OutputStreamWriter 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:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {//from  w  w w. j ava 2 s .c  om

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {//from   w w  w  .j ava  2 s.  c  om

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset());

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {/*from   w w  w  . j av a  2  s. c  o  m*/

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset().newEncoder());

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {//  w ww.j  ava 2  s.c om

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, java.nio.charset.StandardCharsets.UTF_8);

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    char[] arr = { 'H', 'e', 'l', 'l', 'o' };

    try {//  w  ww  .j  av  a2s.  c o m

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os);

        // create a new FileInputStream to read what we write
        FileInputStream in = new FileInputStream("test.txt");

        // write something in the file
        writer.write(arr, 0, 3);

        // flush the stream
        writer.flush();

        // read what we write
        for (int i = 0; i < 3; i++) {
            System.out.print((char) in.read());
        }

        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.netscape.cmsutil.util.Utils.java

public static void copyStream(BufferedReader in, OutputStreamWriter out) throws IOException {
    char[] buf = new char[4096];
    int len;/*from w w w .  ja  va2  s  .  c  o m*/

    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
}

From source file:AIR.Common.Web.FileFtpHandler.java

public static void writeFtp(String ftpFilePath, OutputStream ioO) throws IOException {
    InputStream io = openStream(ftpFilePath);
    OutputStreamWriter outputStream = new OutputStreamWriter(ioO);

    try (InputStreamReader inputStream = new InputStreamReader(io)) {
        int bufferSize = 2048;
        int readCount;
        char[] buffer = new char[bufferSize];

        readCount = inputStream.read(buffer, 0, bufferSize);

        while (readCount > 0) {
            outputStream.write(buffer, 0, readCount);
            readCount = inputStream.read(buffer, 0, bufferSize);
        }/* www . ja v a2 s .co m*/
    }
}

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

/**
 * ?/*from   w  w  w. j  a va 2  s  . c om*/
 *
 * @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:io.mingle.v1.Connection.java

public Response run(String comprehension) {
    String expr = "{ \"query\": \"" + comprehension + "\", \"limit\": 10000 }";
    try {//from  w w  w.  j  av  a2s.c om
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.connect();

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(expr, 0, expr.length());
        out.flush();
        out.close();

        InputStream in = conn.getInputStream();
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        byte[] chunk = new byte[4096];
        int read = 0;
        while ((read = in.read(chunk)) > 0) {
            buf.write(chunk, 0, read);
        }
        in.close();

        String str = buf.toString();
        System.out.println("GOT JSON: " + str);
        return new Response(JSONValue.parse(str));
    } catch (Exception e) {
        System.err.printf("failed to execute: %s\n", expr);
        e.printStackTrace();
    }

    return null;
}

From source file:net.grinder.util.LogCompressUtils.java

/**
 * Compress multiple Files with the given encoding.
 *
 * @param logFiles     files to be compressed
 * @param fromEncoding log file encoding
 * @param toEncoding   compressed log file encoding
 * @return compressed file byte array//from w  ww.  j  a v  a  2 s. c  o m
 */
public static byte[] compress(File[] logFiles, Charset fromEncoding, Charset toEncoding) {
    FileInputStream fis = null;
    InputStreamReader isr = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    OutputStreamWriter osw = null;
    if (toEncoding == null) {
        toEncoding = Charset.defaultCharset();
    }
    if (fromEncoding == null) {
        fromEncoding = Charset.defaultCharset();
    }
    try {
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        osw = new OutputStreamWriter(zos, toEncoding);
        for (File each : logFiles) {
            try {
                fis = new FileInputStream(each);
                isr = new InputStreamReader(fis, fromEncoding);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                char[] buffer = new char[COMPRESS_BUFFER_SIZE];
                int count;
                while ((count = isr.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    osw.write(buffer, 0, count);
                }
                osw.flush();
                zos.flush();
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compressing {} : {}", each.getAbsolutePath(), e.getMessage());
                LOGGER.debug("Details ", e);
            } finally {
                IOUtils.closeQuietly(isr);
                IOUtils.closeQuietly(fis);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compressing log : {} ", e.getMessage());
        LOGGER.debug("Details : ", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(osw);
    }
}