Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PrintWriter AND FileWriter

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
PrintWriter AND FileWriter

FileWriters are usued when we write data into files. but when do we use PrintWriter ? please show me an example or sample code where PrintWriter can be usued but not FileWriter or vice-versa. i just dont understand the difference.
 
The two main ways of doing IO are :
1) On a file
2) To a socket

You can create a printwriter from a sockets input stream, and then write data to it.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
You can create a printwriter from a sockets input stream, and then write data to it.

sedj, i understand these comment is a valuable comment. but i am not getting the whole point. its not clear.

can you please show me a small example in support of ur comment. what it does ? please explain a bit.

thanks for the time.
 
Well, here would be an example of using a PrintWriter to connect to a server running HTTP, and also a PrintWriter to a file :

Code:
		Socket s = new Socket("[URL unfurl="true"]www.acme.com",[/URL] 80);
		OutputStream os = s.getOutputStream();

		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		PrintWriter pw = new PrintWriter(os);
		pw.println("GET /primrose HTTP/1.1");
		pw.println("Accept: text/html;");
		pw.println("Connection: close;");
		pw.println("\r\n\r\n");
		// etc ... other headers & HTTP protocl implementation
		pw.flush();



		PrintWriter pw2 = new PrintWriter(new FileOutputStream("index_downloaded.html"));
		String line = "";
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		  pw2.println(line);
		}
		pw2.flush();

		pw.close();
		pw2.close();
		br.close();
		s.close();

Perhaps you should read an IO tutorial too ?



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Sedj, i hesitate to ask because i dont understand some portion of your code. i tried to understand but failed. i am telling where i have the problem.

i am breaking up ur code into 2 parts.


part 2
------
Code:
PrintWriter pw2 = new PrintWriter(new FileOutputStream("index_downloaded.html"));
        String line = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
          pw2.println(line);
        }
        pw2.flush();

        pw.close();
        pw2.close();
        br.close();
        s.close();


This portion of the code is prints or writes to a file "index_downloaded.html".


part 1
-------

Code:
Socket s = new Socket("[URL unfurl="true"]www.acme.com",[/URL] 80);
        OutputStream os = s.getOutputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter pw = new PrintWriter(os);
        pw.println("GET /primrose HTTP/1.1");// what it is doing ?
        pw.println("Accept: text/html;"); // what it is doing?
        pw.println("Connection: close;");
        pw.println("\r\n\r\n");
        // etc ... other headers & HTTP protocl implementation
        pw.flush();

what this portion is doing ? i dont have the compiler at hand right now so that i can test. i asked my confusion in those comments. its very confusing to me.

i have some guess. you are sending some parameters to the server by these code below

Code:
PrintWriter pw = new PrintWriter(os);
        pw.println("GET /primrose HTTP/1.1"); 
        pw.println("Accept: text/html;"); 
        pw.println("Connection: close;");
        pw.println("\r\n\r\n");
        // etc ... other headers & HTTP protocl implementation

is that right ? no, it can not be done. we can not send Headers to the server like the way you have sent. we send parameters and cookies(kept in the browser) to the server.

this part is creating trouble.


One more thing, u have shown in part 2 using PrintWriter i can download the pages, but you know without using PrintWriter i can download too. is not it ? using just inputstream that can be done. so PrintWriter is not absolute , it is going to be neglected in future by the people becoz there are alternatives.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top