Example usage for java.io PrintWriter write

List of usage examples for java.io PrintWriter write

Introduction

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

Prototype

public void write(String s, int off, int len) 

Source Link

Document

Writes a portion of a string.

Usage

From source file:Main.java

public static void main(String[] args) {
    char[] c = { 'a', 'b', 'c', 'd', 'e', 'f' };

    PrintWriter pw = new PrintWriter(System.out);

    // write char
    pw.write(c, 1, 3);
    pw.write(c, 3, 3);// w  w w .j  a v a2  s  .c o  m

    // flush the writer
    pw.flush();

}

From source file:Main.java

public static void main(String[] args) {
    String s = "tutorial from java2s.com";
    try {//  w  ww  .  ja  v a2 s  .c  o m

        PrintWriter pw = new PrintWriter(System.out);

        // write substrings
        pw.write(s, 0, 5);
        pw.write(s, 6, 5);

        // flush the writer
        pw.flush();

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

From source file:GenericClient.java

public static void main(String[] args) throws IOException {
    try {/*from   w ww .ja va 2  s .  co m*/
        // Check the number of arguments
        if (args.length != 2)
            throw new IllegalArgumentException("Wrong number of args");

        // Parse the host and port specifications
        String host = args[0];
        int port = Integer.parseInt(args[1]);

        // Connect to the specified host and port
        Socket s = new Socket(host, port);

        // Set up streams for reading from and writing to the server.
        // The from_server stream is final for use in the inner class below
        final Reader from_server = new InputStreamReader(s.getInputStream());
        PrintWriter to_server = new PrintWriter(s.getOutputStream());

        // Set up streams for reading from and writing to the console
        // The to_user stream is final for use in the anonymous class below
        BufferedReader from_user = new BufferedReader(new InputStreamReader(System.in));
        // Pass true for auto-flush on println()
        final PrintWriter to_user = new PrintWriter(System.out, true);

        // Tell the user that we've connected
        to_user.println("Connected to " + s.getInetAddress() + ":" + s.getPort());

        // Create a thread that gets output from the server and displays
        // it to the user. We use a separate thread for this so that we
        // can receive asynchronous output
        Thread t = new Thread() {
            public void run() {
                char[] buffer = new char[1024];
                int chars_read;
                try {
                    // Read characters from the server until the
                    // stream closes, and write them to the console
                    while ((chars_read = from_server.read(buffer)) != -1) {
                        to_user.write(buffer, 0, chars_read);
                        to_user.flush();
                    }
                } catch (IOException e) {
                    to_user.println(e);
                }

                // When the server closes the connection, the loop above
                // will end. Tell the user what happened, and call
                // System.exit(), causing the main thread to exit along
                // with this one.
                to_user.println("Connection closed by server.");
                System.exit(0);
            }
        };

        // Now start the server-to-user thread
        t.start();

        // In parallel, read the user's input and pass it on to the server.
        String line;
        while ((line = from_user.readLine()) != null) {
            to_server.print(line + "\r\n");
            to_server.flush();
        }

        // If the user types a Ctrl-D (Unix) or Ctrl-Z (Windows) to end
        // their input, we'll get an EOF, and the loop above will exit.
        // When this happens, we stop the server-to-user thread and close
        // the socket.

        s.close();
        to_user.println("Connection closed by client.");
        System.exit(0);
    }
    // If anything goes wrong, print an error message
    catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java GenericClient <hostname> <port>");
    }
}

From source file:Main.java

public static void putString2XmlFile(String xml, String fileName) {
    PrintWriter out = null;
    try {/*from www. jav  a2s . com*/
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        out.write(xml, 0, xml.length());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:cn.webwheel.results.JsonResult.java

public void render() throws IOException {
    ctx.getResponse().setCharacterEncoding(charset);
    String s = toString();/*from  w  ww  .ja  v  a2 s  .  c  o  m*/
    if (wrapMultipart && ServletFileUpload.isMultipartContent(ctx.getRequest())
            && !"XMLHttpRequest".equals(ctx.getRequest().getHeader("X-Requested-With"))) {
        ctx.getResponse().setContentType(contentType == null ? "text/html" : contentType);
        PrintWriter pw = ctx.getResponse().getWriter();
        final String s1 = "<textarea>";
        final String s2 = "</textarea>";
        pw.write(s1, 0, s1.length());
        pw.write(s);
        pw.write(s2, 0, s2.length());
        return;
    }
    String ct = contentType;
    if (ct == null) {
        ct = "application/json";
        String ua = ctx.getRequest().getHeader("User-Agent");
        if (ua != null && ua.contains("MSIE")) {
            ct = "text/plain;charset=" + charset;
        }
    }
    ctx.getResponse().setContentType(ct);
    ctx.getResponse().getWriter().write(s);
}

From source file:UTF8.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {//  w ww  .  j a  va2s . c  om
        BufferedReader reader = req.getReader();

        res.setContentType("text/html; charset=UTF-8");
        PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

        // Read and write 4K chars at a time
        // (Far more efficient than reading and writing a line at a time)
        char[] buf = new char[4 * 1024]; // 4Kchar buffer
        int len;
        while ((len = reader.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, len);
        }
        out.flush();
    } catch (Exception e) {
        getServletContext().log(e, "Problem filtering page to UTF-8");
    }
}

From source file:com.googlecode.jsfFlex.renderkit.html.util.JsfFlexResourceImpl.java

private void readInputWriteOutput(InputStream resourceStream, HttpServletResponse httpResponse) {

    PrintWriter responseWriter = null;

    try {//w w w.j a  v a  2  s .com
        responseWriter = httpResponse.getWriter();

        BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream));

        char[] charBuffer = new char[2048];
        int offSet = 0;
        while ((offSet = reader.read(charBuffer, 0, 2048)) > -1) {
            responseWriter.write(charBuffer, 0, offSet);
        }

        responseWriter.flush();
    } catch (IOException ioException) {
        _log.debug("IOException while writing the script's content to PrintWriter of HttpServletResponse",
                ioException);
    }

}

From source file:edu.ku.brc.specify.toycode.mexconabio.FileMakerToMySQL.java

/**
 * @param fileName/*  w w w  . j  ava 2  s .  co  m*/
 */
protected void cleanFile(final String fileName) {
    File srcFile = new File(fileName);
    try {
        File destFile = new File("xxx.xml");//File.createTempFile("DB", "xml");
        FileUtils.copyFile(srcFile, destFile);

        //System.out.println("Clean FIle: "+dest)

        FileReader fr = new FileReader(srcFile);
        PrintWriter pw = new PrintWriter(destFile);
        char[] chars = new char[4096 * 8];
        int numChars = fr.read(chars, 0, chars.length);
        while (numChars > 0) {
            for (int i = 0; i < chars.length; i++) {
                //if (chars[i] < 32 || chars[i] > 126)
                if (chars[i] == 0xb) {
                    System.out.println("fixed[" + chars[i] + "]");
                    chars[i] = ' ';

                }
            }
            pw.write(chars, 0, numChars);
            numChars = fr.read(chars, 0, chars.length);
        }

        fr.close();
        pw.close();

        FileUtils.copyFile(destFile, srcFile);

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