List of usage examples for java.io PrintWriter flush
public void flush()
From source file:Connect.java
public static void main(String[] args) { try { // Handle exceptions below // Get our command-line arguments String hostname = args[0]; int port = Integer.parseInt(args[1]); String message = ""; if (args.length > 2) for (int i = 2; i < args.length; i++) message += args[i] + " "; // Create a Socket connected to the specified host and port. Socket s = new Socket(hostname, port); // Get the socket output stream and wrap a PrintWriter around it PrintWriter out = new PrintWriter(s.getOutputStream()); // Sent the specified message through the socket to the server. out.print(message + "\r\n"); out.flush(); // Send it now. // Get an input stream from the socket and wrap a BufferedReader // around it, so we can read lines of text from the server. BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); // Before we start reading the server's response tell the socket // that we don't want to wait more than 3 seconds s.setSoTimeout(3000);/*from www.j a v a 2s.com*/ // Now read lines from the server until the server closes the // connection (and we get a null return indicating EOF) or until // the server is silent for 3 seconds. try { String line; while ((line = in.readLine()) != null) // If we get a line System.out.println(line); // print it out. } catch (SocketTimeoutException e) { // We end up here if readLine() times out. System.err.println("Timeout; no response from server."); } out.close(); // Close the output stream in.close(); // Close the input stream s.close(); // Close the socket } catch (IOException e) { // Handle IO and network exceptions here System.err.println(e); } catch (NumberFormatException e) { // Bad port number System.err.println("You must specify the port as a number"); } catch (ArrayIndexOutOfBoundsException e) { // wrong # of args System.err.println("Usage: Connect <hostname> <port> message..."); } }
From source file:Main.java
public static void main(String[] args) { try {//from w ww . j a v a 2 s.c o m PrintWriter pw = new PrintWriter(new FileWriter("c:/text.txt")); // append chars pw.append('H'); pw.append('e'); pw.append('l'); pw.append('l'); pw.append('o'); // flush the writer pw.flush(); pw.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/*from w ww. jav a2s. c o m*/ PrintWriter pw = new PrintWriter(new File("c:/text.txt"), "ACSII"); // append chars pw.append('H'); pw.append('e'); pw.append('l'); pw.append('l'); pw.append('o'); // flush the writer pw.flush(); pw.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { boolean bool = false; try {/*from w w w .j a va 2 s. c om*/ PrintWriter pw = new PrintWriter(System.out); // print a boolean pw.print(true); // change the line pw.println(); // print another boolean pw.print(bool); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Printf4.java
public static void main(String[] args) { double price = 44.95; double tax = 7.75; double amountDue = price * (1 + tax / 100); PrintWriter out = new PrintWriter(System.out); /* This call will throw an exception--note the %% */ Printf4.fprint(out, "Amount due = %%8.2f\n", amountDue); out.flush(); }/* w w w .j a va 2 s . co m*/
From source file:Main.java
public static void main(String[] args) { try {/*from w w w.java2 s.co m*/ PrintWriter pw = new PrintWriter(new FileWriter("c:/text.txt"), true); // append chars pw.append('H'); pw.append('e'); pw.append('l'); pw.append('l'); pw.append('o'); // flush the writer pw.flush(); pw.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { String s = "tutorial from java2s.com."; try {//ww w .j a va2 s.co m PrintWriter pw = new PrintWriter(System.out); // print string pw.print(s); // change the line pw.println(); // print another string pw.print("This is an example."); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String hostname = "localhost"; Socket theSocket = new Socket(hostname, 7); BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream())); BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(theSocket.getOutputStream()); System.out.println("Connected to echo server"); while (true) { String theLine = userIn.readLine(); if (theLine.equals(".")) break; out.println(theLine);/* w ww. j ava2s .co m*/ out.flush(); System.out.println(networkIn.readLine()); } networkIn.close(); out.close(); }
From source file:Main.java
public static void main(String[] args) { Object obj1 = "Object"; Object obj2 = 2;/* w ww .ja v a 2 s . c o m*/ Date date = new Date(); try { PrintWriter pw = new PrintWriter(System.out); // print object pw.println(obj1); // print another object pw.println(obj2); // print a date (it is an object) pw.print(date); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Properties prop = new Properties(); PrintWriter writer = new PrintWriter(System.out); prop.put("Chapter Count", "200"); prop.put("Tutorial Count", "150"); prop.put("tutorial", "java2s.com"); prop.put("Runnable", "true"); // print the list with a PrintWriter object prop.list(writer);/*from w ww. j a v a2 s . co m*/ // flush the stream writer.flush(); }