How can I create a string stream to write to?

There is the stringwriter

Then there is the printwriter


StringWriter s = new StringWriter();
PrintWriter out = new PrintWriter(s);
...write something to out
StringBuffer output = s.getBuffer();

api for stringbuffer

walking through a string

characteriterator api

Types of Java Output Streams

Search for: Types of Java Output Streams


ByteArrayOutputStream, 
FileOutputStream, 
FilterOutputStream, 
ObjectOutputStream, 
OutputStream, 
PipedOutputStream

Link to Java output stream class

ByteArrayOutputStream

Search for: ByteArrayOutputStream

Show images for: Java outputstream

Amazing what images can tell you!

From explore java book, the chapter on io


private static String getObjectAsString(Object o)
   throws JAXBException
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try   {
         m.marshal(o,bos);
         return bos.toString();
      }
      finally   {
         FileUtils.closeStream(bos);
      }
   }

StringBufferInputStream API

Search for: StringBufferInputStream API

Link to java.io

Java StringReader to an InputStream

Search for: Java StringReader to an InputStream

Here are a set of solutions from SOF


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.ai.common.FileUtils;

/**
 * 
 * This is a test driver to test 
 * to see if the class can be persisted using XML as is
 * 
 * @see LoggedInUserKeysDataset
 * @see PLSUserDetail
 * 
 */
public class PersistentLoginSupportTestDriver 
{
   //Hava static marshallers
   private static JAXBContext jc;
   private static Marshaller m;
   private static Unmarshaller um;
   
   //Initialize them
   static
   {
      try {
         jc = JAXBContext.newInstance(LoggedInUserKeysDataset.class);
         m = jc.createMarshaller();
         um = jc.createUnmarshaller();
         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
      }
      catch(JAXBException x) {
         throw new RuntimeException(x);
      }
   }
   public static void main(String[] args)
   {
      try   {
         test2();
      }
      catch(Exception x)   {
         throw new RuntimeException(x);
      }
   }
  
   private static void test2() throws Exception
   {
      LoggedInUserKeysDataset data = LoggedInUserKeysDataset.createASample();
       
       //Marshal to system output: java to xml
      printMessage("Direct output as XML");
       m.marshal(data,System.out);
       
       //get it as a string first
       String s = getObjectAsString(data);
      printMessage("String output as XML");
      System.out.println(s);
      
      //Read it back as object
      LoggedInUserKeysDataset newData 
      = (LoggedInUserKeysDataset)getStringAsObject(s);
      
       //get it as a string first
       String s1 = getObjectAsString(newData);
      printMessage("String output as XML");
      System.out.println(s1);
   }
   
   private static void printMessage(String x)
   {
      System.out.println("********************************");
      System.out.println(x);
      System.out.println("********************************");
   }
   private static String getObjectAsString(Object o)
   throws JAXBException
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try   {
         m.marshal(o,bos);
         return bos.toString();
      }
      finally   {
         FileUtils.closeStream(bos);
      }
   }
   private static Object getStringAsObject(String s)
   throws JAXBException
   {
      //create a string input stream
      return um.unmarshal(new ByteArrayInputStream(s.getBytes()));
   }
}//eof-class

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LoggedInUserKeysDataset {
   
   /*
    * **************************************
    * Private data
    * **************************************
    */
   @XmlElement (name="PLSUserDetail")
   private List<PLSUserDetail> data = new ArrayList<PLSUserDetail>();
   
   public void addUserDetail(PLSUserDetail ud){data.add(ud);}
   public List<PLSUserDetail> getData() { return data;}
   

   /*
    * **************************************
    * Testing support
    * **************************************
    */
   public static LoggedInUserKeysDataset createASample()
   {
      LoggedInUserKeysDataset lds = new LoggedInUserKeysDataset();
      
      PLSUserDetail ud = createASampleUserDetail("1");
      lds.data.add(ud);
      
      ud = createASampleUserDetail("2");
      lds.data.add(ud);
      
      ud = createASampleUserDetail("3");
      lds.data.add(ud);
      return lds;
   }
   private static PLSUserDetail createASampleUserDetail(String variation)
   {
      //Create user1
      PLSUserDetail ud = new PLSUserDetail("user" + variation);
      ud.addRandomKey("key1");
      ud.addRandomKey("key2");
      return ud;
   }
}//eof-class

Do you have to close byte array streams in Java?

Search for: Do you have to close byte array streams in Java?

FileOutputStream(String name, boolean append)

Creates a file output stream to write to the file with the specified name.