List of usage examples for java.io PrintStream PrintStream
public PrintStream(File file) throws FileNotFoundException
From source file:Main.java
public static void main(String[] args) { File file = new File("C:\\temp\\debug.txt"); try {//from ww w .ja v a 2 s . c o m PrintStream ps = new PrintStream(file); System.setOut(ps); } catch (IOException e) { } System.out.println("To File"); }
From source file:Main.java
public static void main(String[] args) { String destFile = "luci3.txt"; try (PrintStream ps = new PrintStream(destFile)) { ps.println("test"); ps.println("test1"); ps.println("test2"); ps.print("test3"); // flush the print stream ps.flush();/* ww w.jav a 2 s. co m*/ System.out.println("Text has been written to " + (new File(destFile).getAbsolutePath())); } catch (FileNotFoundException e1) { e1.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { File outFile = new File("stdout.txt"); PrintStream ps = new PrintStream(new FileOutputStream(outFile)); System.out.println(outFile.getAbsolutePath()); System.setOut(ps);/* w w w . jav a 2 s . c o m*/ System.out.println("Hello world!"); System.out.println("Java I/O is cool!"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Socket t = new Socket("127.0.0.1", 7); DataInputStream dis = new DataInputStream(t.getInputStream()); PrintStream ps = new PrintStream(t.getOutputStream()); ps.println("Hello"); String str = dis.readUTF();//ww w. ja v a 2 s . c om if (str.equals("Hello")) System.out.println("Alive!"); else System.out.println("Dead"); t.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); Socket sock = ssock.accept(); ssock.close();/*from ww w . j a v a 2 s . c o m*/ PrintStream pstream = new PrintStream(sock.getOutputStream()); for (int i = 100; i >= 0; i--) { pstream.println(i); } pstream.close(); sock.close(); }
From source file:MainClass.java
public static void main(String[] args) { try {// ww w .jav a 2 s .c o m File tempFile = File.createTempFile("myfile", ".tmp"); FileOutputStream fout = new FileOutputStream(tempFile); PrintStream out = new PrintStream(fout); out.println("some text"); } catch (IOException ex) { System.out.println("There was a problem creating/writing to the temp file"); ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { Formatter formatter = new Formatter(new PrintStream(new FileOutputStream("generated/format.txt"))); // format a new string String name = "from java2s.com"; formatter.format("Hello %s !", name); // print the formatted string System.out.println(formatter); // flush the formatter. Here it does nothing. formatter.flush();/*from w w w . j a v a 2s . com*/ System.out.println("Formatter Flushed."); }
From source file:Main.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); Socket sock = ssock.accept(); ssock.close();/* w w w. ja v a 2 s. c om*/ PrintStream pstream = new PrintStream(sock.getOutputStream()); pstream.print("count? "); BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream())); String line = input.readLine(); pstream.println(""); int count = Integer.parseInt(line); for (int i = count; i >= 0; i--) { pstream.println(i); } pstream.close(); sock.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { ServerSocket ss = new ServerSocket(80); while (true) { Socket s = ss.accept();/*from ww w. j a v a2s.co m*/ PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); String info = null; while ((info = in.readLine()) != null) { System.out.println("now got " + info); if (info.equals("")) break; } out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); String c = "<html> <head></head><body> <h1> hi</h1></Body></html>"; out.println("Content_Length:" + c.length()); out.println(""); out.println(c); out.close(); s.close(); in.close(); } }
From source file:MainClass.java
public static void main(String[] args) { try {/*from w w w . j a v a 2 s .co m*/ String FILE = "c:\\systemin.txt"; FileOutputStream outStr = new FileOutputStream(FILE, true); PrintStream printStream = new PrintStream(outStr); System.setErr(printStream); Timestamp now = new Timestamp(System.currentTimeMillis()); System.out.println(now.toString() + ": This is text that should go to the file"); outStr.close(); printStream.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); System.exit(-1); } catch (IOException ex) { ex.printStackTrace(); System.exit(-1); } }