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) throws FileNotFoundException 

Source Link

Document

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

Usage

From source file:Random4.java

public static void main(String[] argv) throws IOException {
    // java.util.Random methods are non-static, do need to construct Math
    Random r = new Random();
    PrintWriter file1 = new PrintWriter(new FileWriter("file1"));
    PrintWriter file2 = new PrintWriter(new FileWriter("file2"));
    for (int i = 0; i < 10000; i++) {
        file1.println(r.nextDouble());//from ww  w . j a  v a  2 s .  c  o m
        file2.println(r.nextGaussian());
    }
    file1.close();
    file2.close();
}

From source file:MainClass.java

License:asdf

public static void main(String[] args) throws IOException {
    try {/*from   w  w  w . j  ava2  s .c o m*/
        BufferedReader in4 = new BufferedReader(new StringReader("asdf"));
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
        int lineCount = 1;
        String s = null;
        while ((s = in4.readLine()) != null)
            out1.println(lineCount++ + ": " + s);
        out1.close();
    } catch (EOFException e) {
        System.err.println("End of stream");
    }

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection dbConn = DriverManager.getConnection(URL, "user", "pass");
    PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    DriverManager.setLogWriter(w);
    dbConn.close();//from w  ww  .j  a va 2 s  . c o  m

    PreparedStatement prepstmt;
    prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
    prepstmt.execute();
    prepstmt.close();
    dbConn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {/*from   w  w w .j  a  v a  2 s.c  o  m*/
        // Load the jdbc-odbc bridge driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Enable logging
        DriverManager.setLogWriter(new PrintWriter((System.err)));

        System.out.println("Getting Connection");
        Connection conn = DriverManager.getConnection(dbURL, "user", "password");

        SQLWarning warn = conn.getWarnings();
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }

        conn.close();
    } catch (ClassNotFoundException e) {
        System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
        System.out.println("Database access failed " + e);
    }
}

From source file:SecureServer.java

public static void main(String[] args) throws Exception {
    ServerSocketFactory ssf = SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(98999);
    Socket sock = ss.accept();/*from  w ww.j a  v  a 2s.co  m*/
    ss.close();
    OutputStream rawOut = sock.getOutputStream();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(rawOut));
    out.println(new java.util.Date().toString());
    out.flush();
    sock.close();

}

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 av  a  2 s . co  m*/
        out.flush();
        System.out.println(networkIn.readLine());
    }
    networkIn.close();
    out.close();
}

From source file:SSLSimpleClient.java

public static void main(String[] args) throws Exception {
    SocketFactory sf = SSLSocketFactory.getDefault();
    Socket s = sf.createSocket(args[0], Integer.parseInt(args[1]));

    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.println("from java2s.");
    pw.flush();/*from www  . j  a v  a2s .  com*/
    System.out.println(br.readLine());
    s.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SocketFactory factory = SSLSocketFactory.getDefault();
    Socket socket = factory.createSocket("127.0.0.1", 8080);

    OutputStream outputStream = socket.getOutputStream();
    PrintWriter out = new PrintWriter(outputStream);
    out.print("GET / HTTP/1.0\r\n\r\n");
    out.flush();//w w w .ja v a 2s  .c om
    InputStream inputStream = socket.getInputStream();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader in = new BufferedReader(inputStreamReader);

    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    out.close();
    in.close();
    socket.close();

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   www  .  j  a  v  a 2s  . c o m
        BufferedReader input = new BufferedReader(new FileReader(args[0]));
        ArrayList list = new ArrayList();
        String line;
        while ((line = input.readLine()) != null) {
            list.add(line);
        }
        input.close();

        Collections.reverse(list);

        PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
        for (Iterator i = list.iterator(); i.hasNext();) {
            output.println((String) i.next());
        }
        output.close();
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:DataFileTest.java

public static void main(String[] args) {
    Employee staff = new Employee("Java Source", 35500);

    staff.raiseSalary(5.25);//from  w ww. j  a  va 2  s.c o  m

    try {
        PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
        writeData(staff, out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
        Employee e = readData(in);
        e.print();
        in.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}