Solved

BufferedWriter , OutputStreamWriter

Posted on 2010-11-19
4
989 Views
Last Modified: 2012-05-10
I am trying to display the the contents of the web page by capturing the output stream.Now the next task is to display the output stream on to console but i am able to get the result as 'java.io.BufferedWriter@14693c7'.
Please guide me what more has to be done in order to display the exact contents of java.io.BufferedWriter@14693c7.

Also tell me how to attach the string strLine with the url, such that when a call to maps.google page is made it should return me the results matching the search criteria.

import java.io.*;

import java.net.*;

import java.util.*;



import com.google.soap.search.GoogleSearch;

import com.google.soap.search.GoogleSearchFault;

import com.google.soap.search.GoogleSearchResult;

import com.google.soap.search.GoogleSearchResultElement;





public class GoogleAPIDemo {

 

  public static void main(String[] args) {

    

    String clientKey ="ABQIAAAA9yfTk5uonWwTfSQcspv6RxTRERdeAiwZ9EeJWta3L_JZVS0bOBTj77GRHnxizMyOhrkoMMDwH9affg";

    String directive = "search"; 

    String directiveArg ="" ;

    // Report the arguments received

    System.out.println("Parameters:"); 

    System.out.println("Client key = " + clientKey);

    System.out.println("Directive  = " + directive);

    System.out.println("Args       = " + directiveArg);



    // Create a Google Search object, set our authorization key

    GoogleSearch search = new GoogleSearch();

    search.setKey(clientKey);

    

    //reading input data from a file

    try {

    FileInputStream fstream = new FileInputStream("list.txt");

    // Get the object of DataInputStream

    DataInputStream in = new DataInputStream(fstream);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;

    //Read File Line By Line

    while ((strLine = br.readLine()) != null)  

    {

      // Print the content on the console

      System.out.println (strLine);

      // Depending on user input, do search or cache query, then print out result

        if (directive.equalsIgnoreCase("search"))

        {

         search.setQueryString(strLine);

         String temp =strLine;

         URL url = new URL("http://maps.google.com");

         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.xxx.com", 8080));

         System.out.println("url is "+url);

         

         URLConnection con = url.openConnection(proxy);

         con.setDoInput(true);

         con.setDoOutput(true);

         con.connect();

         BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));

 		

 	System.out.println("Buffered Writer contents----"+bw1.toString());

 	}

    //Close the input stream

      in.close();

    } 

      catch (GoogleSearchFault f) {

      System.out.println("The call to the Google Web APIs failed:");

      System.out.println(f.toString());

    }

      catch (Exception e){//Catch exception if any

          System.err.println("Error: " + e.getMessage());

        }

  }



  private static void printUsageAndExit() {

    System.err.println("Usage: java " + 

                       GoogleAPIDemo.class.getName() +

                       " <client-key>" +

                       " (search <query> | cached <url> | spell <phrase>)");

    System.exit(1);

  }

}

Open in new window

0
Comment
Question by:ezsas
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 34172125
Collect the output in a StringBuilder
StringBuilder sb = new StringBuilder();

//Read File Line By Line

while ((strLine = br.readLine()) != null)  

{

    sb.append(strline).append("\r\n");

}

String content = sb.toString().trim();

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 34172137
0
 
LVL 92

Expert Comment

by:objects
ID: 34172138
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34218628
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 58
AWS and Java and Rest - newbie question (Is this an authentication issue) 4 68
Java DateChooser? 3 29
javap bin 2 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now