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:Main.java

public static void main(String[] args) {
    String str = "tutorial from java2s.com!";

    Writer writer = new PrintWriter(System.out);

    try {/*  w w w  . j  a v a  2s. c om*/
        // write a string portion
        writer.write(str, 0, 5);

        // flush the writer
        writer.flush();

        // write another string portion
        writer.write(str, 5, 6);

        // flush the stream again
        writer.flush();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    char[] c = { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    Writer writer = new PrintWriter(System.out);

    try {//from  w  ww . j  a  v  a  2 s .c o m
        // write a portion of a char array
        writer.write(c, 0, 5);

        // flush the writer
        writer.flush();

        // write another portion of a char array
        writer.write(c, 5, 5);

        // flush the stream again
        writer.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Read input from reader and write it to writer until there is no more
 * input from reader.//from  w  ww.j  a  v  a  2 s . c o m
 *
 * @param reader the reader to read from.
 * @param writer the writer to write to.
 * @param buf the char array to use as a bufferx
 */
public static void flow(Reader reader, Writer writer, char[] buf) throws IOException {
    int numRead;
    while ((numRead = reader.read(buf)) >= 0) {
        writer.write(buf, 0, numRead);
    }
}

From source file:Main.java

public static void transfer(Reader reader, Writer writer) throws IOException {

    char[] buf = new char[8192];
    int count = 0;

    while ((count = reader.read(buf)) >= 0) {

        writer.write(buf, 0, count);
    }//  w  w w.  ja  va 2  s.c o m
    writer.flush();
}

From source file:Main.java

public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;/*from  w ww .  j a  va 2 s. c  o  m*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static long copy(@NonNull Reader input, @NonNull Writer output) throws IOException {
    char[] buffer = new char[1024 * 4];
    long count = 0;
    int n = 0;/*ww w . ja v  a2s  .c  o  m*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[4096];
    long count = 0L;
    int n = 0;/*from   w w  w  . j  a v a 2 s.c  o m*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:Main.java

/**
 * Transfers all characters that can be read from <tt>in</tt> to
 * <tt>out</tt>.//from w ww .j  av a2 s  .c om
 *
 * @param in The Reader to read characters from.
 * @param out The Writer to write characters to.
 * @return The total number of characters transfered.
 */
public static final long transfer(Reader in, Writer out) throws IOException {
    long totalChars = 0;
    int charsInBuf = 0;
    char[] buf = new char[4096];

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

    return totalChars;
}

From source file:Main.java

/**
 * Copy a reader to writer./*from   ww w.j  ava 2  s  . c  o m*/
 */
private static void copy(Reader in, Writer out) throws IOException {
    try {
        int bytesRead;
        char[] buffer = new char[COPY_BUFFER_SIZE];

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

        out.flush();
    } finally {
        in.close();
        out.close();
    }
}

From source file:Main.java

public static void copyCompletely(Reader input, Writer output) throws IOException {
    char[] buf = new char[8192];
    while (true) {
        int length = input.read(buf);
        if (length < 0)
            break;
        output.write(buf, 0, length);
    }/* www  . j  a v  a2  s  .  c  o m*/

    try {
        input.close();
    } catch (IOException ignore) {
    }
    try {
        output.close();
    } catch (IOException ignore) {
    }
}