How to read a server side file?

Posted on 2003-02-24
Medium Priority
Last Modified: 2010-03-31
I have a servlet application and I need to read a file (simple ascii file) from a valid url (example: http://google.com/book/list.txt), how can I read this file? using FileReader??


Question by:javagirip
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 8011193
Open a URLConnection on it and get its input stream
LVL 18

Accepted Solution

bobbit31 earned 200 total points
ID: 8011214
BufferedReader in = null;

try {
    URL myURL = new URL("http://google.com/book/list.txt"));
    URLConnection conn = myURL.openConnection();

    String s;
    String fileText = "";
    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
   while ((s = in.readLine()) != null) {
       fileText += s;
} catch (Exception e) {

Expert Comment

ID: 8015196
Well, because it is a servlet, I am sure you want to send what ever you read from the URL to a client browser that made the request.
To do this connect the output stream of your servlet to the inputstream from the URL connection.
in other words..
  while ((s = in.readLine()) != null) {
      out.println( s );

This will automatically transfer what you read to the waiting client browser..
I hope this helps..


Author Comment

ID: 8016416
It worked thanks!

One more question, can I use the same approach to read the file name in a directory? say, I have directory "book" at "http://google.com/book/" how do I read all the file names in that directory?
LVL 18

Expert Comment

ID: 8017424
the above code will just store whatever output the url gives... so if you go to http://google.com/book/ in your browser, and it serves you something, then that would be what gets stored in your string.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 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