• Post Reply Bookmark Topic Watch Topic
  • New Topic

java IO

 
Janaki Samaraweera
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there possibility of converting string to stream?
Can some one help me soon!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"js",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of ways to do this, possibly the most direct is to convert the String to a byte array using String.getBytes() then write the array of bytes to any OutputStream.
Dave
 
Janaki Samaraweera
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
There are a number of ways to do this, possibly the most direct is to convert the String to a byte array using String.getBytes() then write the array of bytes to any OutputStream.
Dave



thanks David, But my exact requirement is like this. I have a String and I want to covert it to inputstream.
(String s = "String that I am geting from db"
 
Janaki Samaraweera
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
There are a number of ways to do this, possibly the most direct is to convert the String to a byte array using String.getBytes() then write the array of bytes to any OutputStream.
Dave


String s = "Hello";
ByteArrayInputStream bin = new ByteArrayInputStream(s.getBytes());
ObjectInputStream oin = new ObjectInputStream(bin);
System.out.println(oin.readObject());
IS this waht you are telling?
Tjhis is giving me java.io.StreamCorruptedException: invalid stream header
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ObjectInputStream is something entirely different. If the contents of a stream weren't originally created with an ObjectOutputStream, it won't mean anything. And I expect it uses a completely different format than what you're probably expecting anyway. Ignore it.
If what you want really is to convert a String to an InputStream - you did that on the second line:

A ByteArrayInputStream is an InputStream. Not sure if that's really what you want though...
[ August 20, 2002: Message edited by: Jim Yingst ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are willing to live with the coversion of chars to bytes why not just use the StringBufferInputStream? Otherwise use the StringReader.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, StringBufferInputStream is deprecated. (I know, that's why you said "if you're willing to live with...") The getBytes() / ByteArrayInputStream method does the same thing, but can be easily modified to use a different encoding if desired. But yeah, the whole encoding issue is suspect, and it seems like a StringReader may well be what's really needed...
[ August 20, 2002: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!