Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

java.net.URLConnection example?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know where I can find an example of reading in a text file with a java.net.URLConnection? I've read the doc's on suns java page but I did not find them that much help to be honest. I've also googled for answers using "java.net.URLConnection" as the search, but this didn't provide any help.

cheers
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this (jdk 5/1.5) Core Java Technologies Tech Tip - Scanning Text wtih java.util.Scanner

The tech tip references this blog entry that shows a BufferedReader version and a Scanner version.
[ March 09, 2005: Message edited by: Carol Enderlin ]
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks carol, that was great info
however, my Java SDK is version 1.4.2, and it doesn't seem to have the scanner utility.....
Ill search some more, but again, thankyou very much that was a great help!
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

There is an article here that discusses writing a plain-text content handler with URLConnection.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Horatio, I'll check that link out in a minute.
At the moment I'm having an annoyance with reading from a DataInputStream.
I'm trying to read in a plain text file, about 85kb, but sometimes it does not read it all....it mostly finishes the file too early.
Here is the code I have:

import java.io.*;
import java.net.*;

class fileReader {//start of class

public static void main (String[] args) {//main method
try{
InputStream fileSource = new URL("http://members.aol.com/davllew/teapot.x3d").openStream();
byte [] buf = new byte[fileSource.available()];
new DataInputStream(fileSource).readFully(buf);
String text = new String(buf);
System.out.println(text);
fileSource.close();
}
catch(IOException e){
System.out.println("Error reading file!");
}

}//main method

}//end of class


Is this a well known problem? and If so, I wonder how to avoid it.
cheers again
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, lets have a look at your code:

byte [] buf = new byte[fileSource.available()];

available() isn't the same as length.

new DataInputStream(fileSource).readFully(buf);

The API for readFully(byte[] b) says the following:

"Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b."

As your buffer may not actually be the length of the file, this may not actually read all of it.

As you want to read a text file into a string, the best way to proceed would be to wrap your input string in a buffer, then read lines from the buffer, appending them to a string buffer until the are none left. Then get the string from the contents of the string buffer.

Here's a snippet you can draw inspiration from:


 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that Horatio, its been most helpful
from the snippet I've managed to do what I wanted which is great.
In case your interested, or there's a better way, this is what I'm now doing:


Once again, thankyou very much!
 
reply
    Bookmark Topic Watch Topic
  • New Topic