Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 20, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reading from URL into String

What is the maximum size of a string array I can have in my applet? I am trying to store some stock prices in a string array after reading them from a URL connection, but I get an applet exception. What is going wrong? Any suggestions?

I suspect you're trying to read the contents of the URL using URLConnection.getContent() and that this is not working for you. Netscape's implementation of getContent() is known to have bugs when it's dealing with simple text files. A better way to read the data into your applet is to use InputStreams.

For example, if your stock data were stored in a text file called "stock.txt" on the Web server and you wanted your applet to read the contents of "stock.txt" into a string using a URL, you would do the following:

       String inputLine;
       String stockData = "";

       URL stockURL = new URL(getDocumentBase(), "stock.txt"));
       DataInputStream s = new DataInputStream(stockURL.openStream());

       while ((inputLine = s.readLine()) != null) {
               stockData = stockData + inputLine;

This will read the contents of "stock.txt" into a single String object and allocate memory for it as needed. The String will grow to accommodate the amount of data in the data file. You can then use StringTokenizer to read the data out of the String and parse it as you see fit.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date