stream and string array

Discussion in 'Java' started by focode, Aug 6, 2009.

  1. focode

    focode Guest

    i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/
    abc.php" then oped a input stream on this connection , read the data
    in byte[] data = new byte[length];
    in.read(data);
    then converted this into string by
    String response = new String(data);
    and then displays it ..
    the content of the response is arunesh class1 office ...etc
    i want to collect this string individually in string array such that
    str[1] = arunesh
    str[2]=class1
    str[3]=office and so on ..

    i can do this by reading the space and putting some logic to separate
    individual string ...Is their any stander ed way to do this in java ?
     
    focode, Aug 6, 2009
    #1
    1. Advertising

  2. focode

    Simon Guest

    > i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/
    > abc.php"


    Does not work for me.

    > then oped a input stream on this connection , read the data
    > in byte[] data = new byte[length];
    > in.read(data);


    Are you aware that InputStream.read(byte[]) does not necessarily read
    the entire buffer?

    > then converted this into string by
    > String response = new String(data);


    Since you are taking this from a HTTP connection you probably want to
    specify a Charset explicitly:

    new String(data, someCharset)

    You can as well wrap a Reader around your InputStream (with the
    appropriate encoding). In that case you don't have to handle the
    buffering etc. yourself. BufferedReader has a readLine method which
    seems appropriate for your application.

    > and then displays it ..
    > the content of the response is arunesh class1 office ...etc
    > i want to collect this string individually in string array such that
    > str[1] = arunesh
    > str[2]=class1
    > str[3]=office and so on ..


    If you want to split at whitespace, you can use String.split(regexp);
    You can also look at

    http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html

    or

    http://java.sun.com/javase/6/docs/api/java/io/StreamTokenizer.html

    if this is more appropriate.

    Cheers,
    Simon
     
    Simon, Aug 6, 2009
    #2
    1. Advertising

  3. focode

    focode Guest

    On Aug 6, 1:05 pm, Simon <> wrote:
    > > i have opened an HttpConnection to my url (www.schoolssmart.in/mdm/
    > > abc.php"

    >
    > Does not work for me.
    >
    > > then oped a input stream on this connection , read the data
    > > in byte[] data = new byte[length];
    > > in.read(data);

    >
    > Are you aware that InputStream.read(byte[]) does not necessarily read
    > the entire buffer?
    >
    > > then converted this into string by
    > > String response = new String(data);

    >
    > Since you are taking this from a HTTP connection you probably want to
    > specify a Charset explicitly:
    >
    >    new String(data, someCharset)
    >
    > You can as well wrap a Reader around your InputStream (with the
    > appropriate encoding). In that case you don't have to handle the
    > buffering etc. yourself. BufferedReader has a readLine method which
    > seems appropriate for your application.
    >
    > > and then displays it ..
    > > the content of the response is arunesh class1 office ...etc
    > > i want to collect this string individually in string array such that
    > > str[1] = arunesh
    > > str[2]=class1
    > > str[3]=office and so on ..

    >
    > If you want to split at whitespace, you can use String.split(regexp);
    > You can also look at
    >
    > http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html
    >
    > or
    >
    > http://java.sun.com/javase/6/docs/api/java/io/StreamTokenizer.html
    >
    > if this is more appropriate.
    >
    > Cheers,
    > Simon


    Dear Simon
    i appreciate your help.
    the url is "http://schoolsmart.in/mdm/abc.txt" their are
    strings i can read them well , so no buffer problem , but i am working
    on j2me platform and it doesn't support StringTokenizer nor
    StreamTokenizer (all packages are not available fron j2se ) so if any
    another way .....!
     
    focode, Aug 6, 2009
    #3
  4. focode

    Simon Guest

    > the url is "http://schoolsmart.in/mdm/abc.txt" their are
    > strings i can read them well , so no buffer problem , but i am working
    > on j2me platform and it doesn't support StringTokenizer nor
    > StreamTokenizer (all packages are not available fron j2se ) so if any
    > another way .....!


    And what about the first option, String.split()? Of course you have to
    be careful because you cannot easily escape whitespace with this mechanism.

    Cheers,
    Simon
     
    Simon, Aug 6, 2009
    #4
  5. focode

    focode Guest

    On Aug 6, 1:54 pm, Simon <> wrote:
    > >           the url is "http://schoolsmart.in/mdm/abc.txt" their are
    > > strings i can read them well , so no buffer problem , but i am working
    > > on j2me platform and it doesn't support  StringTokenizer nor
    > > StreamTokenizer (all packages are not available fron j2se ) so if any
    > > another way .....!

    >
    > And what about the first option, String.split()? Of course you have to
    > be careful because you cannot easily escape whitespace with this mechanism.
    >
    > Cheers,
    > Simon


    no dear String.split()? also not works i will have to find another way
    out may be write a method for it ....!!!!!!
     
    focode, Aug 6, 2009
    #5
  6. Simon wrote:
    >
    > Since you are taking this from a HTTP connection you probably want to
    > specify a Charset explicitly:
    >
    > new String(data, someCharset)


    Isn't the charset specified in the server response? If so, selecting a
    charset other than the one used by the server would cause him problems I
    should think.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 6, 2009
    #6
  7. focode

    Simon Guest

    >> Since you are taking this from a HTTP connection you probably want to
    >> specify a Charset explicitly:
    >>
    >> new String(data, someCharset)

    >
    > Isn't the charset specified in the server response? If so, selecting a
    > charset other than the one used by the server would cause him problems I
    > should think.


    Exactly, but since he is reading only bytes, new String(byteBuffer) will
    not know which charset the server specified, will it? Or am I missing
    something?

    To be more clearly, I should not have said "a Charset", but rather the
    one returned by connection.getContentEncoding().

    Cheers,
    Simon
     
    Simon, Aug 6, 2009
    #7
  8. Simon wrote:
    >>> Since you are taking this from a HTTP connection you probably want to
    >>> specify a Charset explicitly:
    >>>
    >>> new String(data, someCharset)

    >>
    >> Isn't the charset specified in the server response? If so, selecting
    >> a charset other than the one used by the server would cause him
    >> problems I should think.

    >
    > Exactly, but since he is reading only bytes, new String(byteBuffer) will
    > not know which charset the server specified, will it? Or am I missing
    > something?
    >
    > To be more clearly, I should not have said "a Charset", but rather the
    > one returned by connection.getContentEncoding().


    I haven't played with URLConnection much but I believe
    getContentEncoding() will return the charset used by the server. So yes
    I agree that he needs to specify the charset from the connection in the
    String constructor.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 6, 2009
    #8
  9. Steven Simpson wrote:
    > Knute Johnson wrote:
    >> Simon wrote:
    >>> To be more clearly, I should not have said "a Charset", but rather
    >>> the one returned by connection.getContentEncoding().

    >> I haven't played with URLConnection much but I believe
    >> getContentEncoding() will return the charset used by the server. So
    >> yes I agree that he needs to specify the charset from the connection
    >> in the String constructor.

    >
    > No, getContentEncoding() returns things like gzip or compress.
    >
    > getContentType() returns the MIME type. If text/*, it might have a
    > charset parameter on the end of it, e.g. text/html; charset=ISO-8859-1.
    >
    > Content-Encoding:
    > <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11>
    >
    > Content-Type:
    > <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17>
    >


    Excellent, I knew it had to be there somewhere. I just didn't know where.

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 6, 2009
    #9
  10. focode

    Simon Guest

    > No, getContentEncoding() returns things like gzip or compress.
    >
    > getContentType() returns the MIME type. If text/*, it might have a
    > charset parameter on the end of it, e.g. text/html; charset=ISO-8859-1.


    Oops, thank you for correcting this. Shame on me.

    Cheers,
    Simon
     
    Simon, Aug 7, 2009
    #10
  11. focode

    Roedy Green Guest

    On Thu, 06 Aug 2009 20:05:57 +0100, Steven Simpson <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >No, getContentEncoding() returns things like gzip or compress.
    >
    >getContentType() returns the MIME type. If text/*, it might have a
    >charset parameter on the end of it, e.g. text/html; charset=ISO-8859-1.


    see http://mindprod.com/products1.html#HTTP

    its GET class lets you specify your preferred encoding, but decodes
    the response based on the actual encoding returned by the server in
    the header.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "We must be very careful when we give advice to younger people: sometimes
    they follow it!"
    ~ Edsger Wybe Dijkstra, born: 1930-05-11 died: 2002-08-06 at age: 72
     
    Roedy Green, Aug 10, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    760
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    640
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    456
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    558
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    399
    James Kanze
    May 10, 2007
Loading...

Share This Page