Example usage for java.io PrintWriter PrintWriter

List of usage examples for java.io PrintWriter PrintWriter

Introduction

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

Prototype

public PrintWriter(File file, Charset charset) throws IOException 

Source Link

Document

Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.

Usage

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {//from ww  w.  j a v  a 2s .co  m
        // Get a reader to read the incoming data
        BufferedReader reader = req.getReader();

        // Get a writer to write the data in UTF-8
        res.setContentType("text/html; charset=UTF-8");
        //PrintWriter out = res.getWriter();
        out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

        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) {
        out.println("Problem filtering page to UTF-8");
        getServletContext().log(e, "Problem filtering page to UTF-8");
    }
    out.println("Done");
}

From source file:UTF8.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {//from  w w w. j  a  v a  2  s. c  o m
        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:TimeServer.java

public void run() {
    Socket client = null;//from ww w .  ja  va2  s .  co  m

    while (true) {
        if (sock == null)
            return;
        try {
            client = sock.accept();
            BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
            PrintWriter os = new PrintWriter(bos, false);
            String outLine;

            Date now = new Date();
            os.println(now);
            os.flush();

            os.close();
            client.close();
        } catch (IOException e) {
            System.out.println("Error: couldn't connect to client.");
            System.exit(1);
        }
    }
}

From source file:Client.ClientHandler.java

public void run() {
    try {// ww  w . ja  v  a2  s .  c om
        PrintWriter out = null;
        BufferedReader in = null;
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        out.println("Welcome to the capitalization server");
        out.println("Your client Id is " + clientID);

        while (true) {
            String input = in.readLine();
            if (input.equals("") || input.equals(".")) {
                break;
            }
            out.println(input.toUpperCase());
        }

    } catch (IOException ex) {
        Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        IOUtils.closeQuietly(socket);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }

}

From source file:de.tudarmstadt.lt.lm.app.GenerateNgrams.java

static void print_usage(Options opts, String message) {
    if (message != null)
        System.out.println(message);
    if (opts != null)
        new HelpFormatter().printHelp(new PrintWriter(System.out, true), 80,
                "... " + GenerateNgrams.class.getName() + " <options>", "Options", opts, 2, 2, "======");
    System.exit(1);//w  w  w  .  j a  v a  2s. com
}

From source file:EchoClient.java

public static void main(String[] args) throws IOException {

        Socket kkSocket = null;//w ww .  jav a 2  s  .  co  m
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("taranis", 4444);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
            if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
            }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }

From source file:examples.echo.java

public static final void echoTCP(String host) throws IOException {
    EchoTCPClient client = new EchoTCPClient();
    BufferedReader input, echoInput;
    PrintWriter echoOutput;//from   w ww .j  a  v  a2  s  .c  om
    String line;

    // We want to timeout if a response takes longer than 60 seconds
    client.setDefaultTimeout(60000);
    client.connect(host);
    System.out.println("Connected to " + host + ".");
    input = new BufferedReader(new InputStreamReader(System.in));
    echoOutput = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
    echoInput = new BufferedReader(new InputStreamReader(client.getInputStream()));

    while ((line = input.readLine()) != null) {
        echoOutput.println(line);
        System.out.println(echoInput.readLine());
    }

    client.disconnect();
}

From source file:Client.Connection.java

public Connection(String host, int port) throws IOException {
    s = new Socket(host, port);
    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    out = new PrintWriter(s.getOutputStream(), true);
}

From source file:info.hieule.framework.laravel.utils.LaravelSecurityString.java

public static void updateSecurityString(FileObject configFile) throws IOException {
    List<String> lines = configFile.asLines();
    String newKey = RandomStringUtils.randomAlphanumeric(32);
    OutputStream outputStream = configFile.getOutputStream();
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); // NOI18N
    try {/*  w w w. j a va  2  s.  c o  m*/
        for (String line : lines) {
            if (line.contains(_SECURITY_STRING_LINE)) {
                line = String.format(_SECURITY_STRING_FORMAT, newKey);
            }
            pw.println(line);
        }
    } finally {
        outputStream.close();
        pw.close();
    }
}

From source file:name.martingeisse.ecobuild.util.buildlog.DynamicHtmlFlatTreeLogOrganizer.java

/**
 * Constructor./*from   ww  w  .  j  av a  2 s .c om*/
 * @param w the writer to which the HTML is written
 */
public void setOutputWriter(Writer w) {
    this.out = new PrintWriter(w, true);
}