File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes java equivalent of PHP echo file_get_contents('http://www.example.com/') command? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java equivalent of PHP echo file_get_contents( Watch "java equivalent of PHP echo file_get_contents( New topic
Author

java equivalent of PHP echo file_get_contents('http://www.example.com/') command?

nathan sullivan
Greenhorn

Joined: Dec 20, 2010
Posts: 3
What is the equivalent command of echo file_get_contents('http://www.example.com/') in java?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

There is no one-method equivalent. You can do it with a few methods though:
To increase performance you can use a BufferedInputStream or BufferedReader in line 2 of the above code, and possibly create the StringWriter with the content-length of the URL's URLConnection. You would need to change lines 2-3 into this:
You would need to check if length is not -1 though, or creating the StringWriter would fail.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61082
    
  66

If you're doing this in a JSP, then you'd use <c:import>.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java equivalent of PHP echo file_get_contents('http://www.example.com/') command?