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

License:asdf

public static void main(String[] arges) throws Exception {

    InetAddress addr = InetAddress.getByName(null);
    Socket sk = new Socket(addr, 8888);
    BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())), true);
    out.println("asdf");
    System.out.println(in.readLine());
}

From source file:Logging.java

public static void main(String args[]) throws Exception {
    FileOutputStream errors = new FileOutputStream("StdErr.txt", true);
    PrintStream stderr = new PrintStream(errors);
    PrintWriter errLog = new PrintWriter(errors, true);
    System.setErr(stderr);/*  www .  j a v a2 s  . co m*/

    String query = "SELECT Name,Description,Qty,Cost,Sell_Price FROM Stock";

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String name = rs.getString("Name");
            String desc = rs.getString("Description");
            int qty = rs.getInt("Qty");
            float cost = rs.getFloat("Cost");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace(errLog);
    } catch (SQLException e) {
        System.err.println((new GregorianCalendar()).getTime());
        System.err.println("Thread: " + Thread.currentThread());
        System.err.println("ErrorCode: " + e.getErrorCode());
        System.err.println("SQLState:  " + e.getSQLState());
        System.err.println("Message:   " + e.getMessage());
        System.err.println("NextException: " + e.getNextException());
        e.printStackTrace(errLog);
        System.err.println();
    }
    stderr.close();
}

From source file:BufferedSocketClient.java

  public static void main(String args[]) throws Exception {
  Socket socket1;//from www  .  j a v a  2  s.c o m
  int portNumber = 1777;
  String str = "initialize";

  socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

  BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));

  PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true);

  pw.println(str);

  while ((str = br.readLine()) != null) {
    System.out.println(str);
    pw.println("bye");

    if (str.equals("bye"))
      break;
  }

  br.close();
  pw.close();
  socket1.close();
}

From source file:POP3Demo.java

public static void main(String[] args) throws Exception {
    int POP3Port = 110;
    Socket client = new Socket("127.0.0.1", POP3Port);
    InputStream is = client.getInputStream();
    BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
    OutputStream os = client.getOutputStream();
    PrintWriter sockout = new PrintWriter(os, true);
    String cmd = "user Smith";
    sockout.println(cmd);/*  ww  w . j av a 2 s .  c om*/
    String reply = sockin.readLine();
    cmd = "pass ";
    sockout.println(cmd + "popPassword");
    reply = sockin.readLine();
    cmd = "stat";
    sockout.println(cmd);
    reply = sockin.readLine();
    if (reply == null)
        return;
    cmd = "retr 1";
    sockout.println(cmd);
    if (cmd.toLowerCase().startsWith("retr") && reply.charAt(0) == '+')
        do {
            reply = sockin.readLine();
            System.out.println("S:" + reply);
            if (reply != null && reply.length() > 0)
                if (reply.charAt(0) == '.')
                    break;
        } while (true);
    cmd = "quit";
    sockout.println(cmd);
    client.close();
}

From source file:medcheck.Medcheck.java

/**
 * @param args the command line arguments
 *///from w w  w . j  a  v a  2s. co m
public static void main(String[] args) {
    String jsonFileLocation = "meddata.json";
    File jsonTxt = new File(jsonFileLocation);

    String xmlFileLocation = "XML Files";
    File xmlDirectory = new File(xmlFileLocation);

    String jsonString = getJSONString(jsonTxt);

    PrintWriter csvWriter;
    try {
        csvWriter = new PrintWriter("meddata.csv", "UTF-8");
    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
        e.printStackTrace();
        return;
    }

    Drug[] csvArray = convertToDrugArrayFromJSON(jsonString);
    Drug[] xmlArray = convertToDrugArrayFromXML(xmlDirectory);
    String csvString = convertToCsvString(csvArray, xmlArray);
    csvWriter.print(csvString);
    csvWriter.close();
}

From source file:BufferedSocketClient.java

public static void main(String args[]) throws Exception {
        int cTosPortNumber = 1777;
        String str;//  w  ww .j a  va2 s .c  o  m

        ServerSocket servSocket = new ServerSocket(cTosPortNumber);
        System.out.println("Waiting for a connection on " + cTosPortNumber);

        Socket fromClientSocket = servSocket.accept();
        PrintWriter pw = new PrintWriter(fromClientSocket.getOutputStream(), true);

        BufferedReader br = new BufferedReader(new InputStreamReader(fromClientSocket.getInputStream()));

        while ((str = br.readLine()) != null) {
            System.out.println("The message: " + str);

            if (str.equals("bye")) {
                pw.println("bye");
                break;
            } else {
                str = "Server returns " + str;
                pw.println(str);
            }
        }
        pw.close();
        br.close();

        fromClientSocket.close();
    }

From source file:net.socket.bio.TimeClient.java

/**
 * @param args/* w w w  .  j a  va 2 s .  c  o m*/
 */
public static void main(String[] args) {

    int port = 8089;
    if (args != null && args.length > 0) {

        try {
            port = Integer.valueOf(args[0]);
        } catch (NumberFormatException e) {
            // 
        }

    }
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try {
        socket = new Socket("127.0.0.1", port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
        out.println("QUERY TIME ORDER");
        out.println("QUERY TIME ORDER");
        String test = StringUtils.repeat("hello tcp", 1000);
        out.println(test);
        System.out.println("Send order 2 server succeed.");
        String resp = in.readLine();
        System.out.println("Now is : " + resp);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(socket);
    }
}

From source file:cloudclient.Client.java

public static void main(String[] args) throws Exception {
    //Command interpreter
    CommandLineInterface cmd = new CommandLineInterface(args);

    String socket = cmd.getOptionValue("s");
    String Host_IP = socket.split(":")[0];
    int Port = Integer.parseInt(socket.split(":")[1]);
    String workload = cmd.getOptionValue("w");

    try {//from w  ww . ja v a  2  s  .  c o  m
        // make connection to server socket 
        Socket client = new Socket(Host_IP, Port);

        InputStream inStream = client.getInputStream();
        OutputStream outStream = client.getOutputStream();
        PrintWriter out = new PrintWriter(outStream, true);
        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

        System.out.println("Send tasks to server...");
        //Start clock
        long startTime = System.currentTimeMillis();

        //Batch sending tasks
        batchSendTask(out, workload);
        client.shutdownOutput();

        //Batch receive responses
        batchReceiveResp(in);

        //End clock
        long endTime = System.currentTimeMillis();
        double totalTime = (endTime - startTime) / 1e3;

        System.out.println("\nDone!");
        System.out.println("Time to execution = " + totalTime + " sec.");

        // close the socket connection
        client.close();

    } catch (IOException ioe) {
        System.err.println(ioe);
    }

}

From source file:EchoServer.java

public static void main(String[] args) {
    try {/*  w w w.java 2 s.co  m*/
        // establish server socket
        ServerSocket s = new ServerSocket(8189);

        // wait for client connection
        Socket incoming = s.accept();
        try {
            InputStream inStream = incoming.getInputStream();
            OutputStream outStream = incoming.getOutputStream();

            Scanner in = new Scanner(inStream);
            PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);

            out.println("Hello! Enter BYE to exit.");

            // echo client input
            boolean done = false;
            while (!done && in.hasNextLine()) {
                String line = in.nextLine();
                out.println("Echo: " + line);
                if (line.trim().equals("BYE"))
                    done = true;
            }
        } finally {
            incoming.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.cloudera.knittingboar.conf.cmdline.DataConverterCmdLineDriver.java

public static void main(String[] args) throws Exception {
    mainToOutput(args, new PrintWriter(System.out, true));
}