Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

In this page you can find the example usage for java.io StringWriter write.

Prototype

public void write(String str) 

Source Link

Document

Write a string.

Usage

From source file:Main.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter(100);
    String s = "This is a test from j a va2s.com.";
    for (int i = 0; i < s.length(); ++i) {
        outStream.write(s.charAt(i));
    }/*  ww  w  .  ja  v  a  2  s  .co m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
}

From source file:com.discursive.jccook.slide.GetExample.java

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    String url = "http://www.discursive.com/jccook/dav/test.html";
    Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass");

    // List resources in top directory
    WebdavResource resource = new WebdavResource(url, credentials);

    System.out.println("The three ways to Read resources.");

    // Read to a temporary file
    File tempFile = new File(resource.getName());
    resource.getMethod(tempFile);//from   w ww .j a v a2s .  c o  m
    System.out.println("1. " + resource.getName() + " saved in file: " + tempFile.toString());

    // Read as a String
    String resourceData = resource.getMethodDataAsString();
    System.out.println("2. Contents of " + resource.getName() + " as String.");
    System.out.println(resourceData);

    // Read with a stream
    InputStream resourceStream = resource.getMethodData();
    Reader reader = new InputStreamReader(resourceStream);
    StringWriter writer = new StringWriter();
    while (reader.ready()) {
        writer.write(reader.read());
    }
    System.out.println("3. Contents of " + resource.getName() + " from InputStream.");
    System.out.println(writer.toString());

    resource.close();

}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // create a new sequence
    String s = "Hello from java2s.com";

    // write a string
    sw.write(s);

    System.out.println(sw.toString());

    try {//from w  w  w . ja  v  a2  s  .c om
        // close the writer
        sw.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        ;
    }

}

From source file:org.mzd.shap.analysis.metagene.Metagene2008ToXML.java

public static void main(String[] args) throws IOException, BeanIOException {
    if (args.length != 2) {
        System.out.println("[input sequence] [output orfs]");
        System.exit(1);/*from ww w  . java  2 s. co m*/
    }

    Process p = Runtime.getRuntime().exec("metagene " + args[0]);
    try {
        synchronized (p) {
            StringWriter sw = new StringWriter();
            InputStreamReader isr = new InputStreamReader(p.getInputStream());
            int retVal;
            while (true) {
                p.wait(100);
                while (isr.ready()) {
                    sw.write(isr.read());
                }
                try {
                    retVal = p.exitValue();
                    break;
                } catch (IllegalThreadStateException ex) {
                    /*...*/}
            }

            // Just make sure stdout is completely empty.
            while (isr.ready()) {
                sw.write(isr.read());
            }

            if (retVal != 0) {
                System.out.println("Non-zero exist status [" + retVal + "]");
                InputStream is = null;
                try {
                    is = p.getErrorStream();
                    while (is.available() > 0) {
                        System.out.write(is.read());
                    }
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            } else {
                new Metagene2008ToXML().convert(sw.toString(), new File(args[1]));
            }

            System.exit(retVal);
        }
    } catch (InterruptedException ex) {
        /*...*/}
}

From source file:Main.java

public static String dumpException(Throwable e) {
    StringWriter sw = new StringWriter(160);
    sw.write(e.getClass().getName());
    sw.write(":\n");
    e.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * dump the exception to string//from   ww w . ja v a 2s. com
 */
public static String dumpException(Throwable throwable) {
    StringWriter stringWriter = new StringWriter(160);
    stringWriter.write(throwable.getClass().getName());
    stringWriter.write(":\n");
    throwable.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:Main.java

public static String inputStreamToString(InputStream inputStream) throws IOException {
    StringWriter sw = new StringWriter();
    int c;/*from  w w  w  . j  a  v a 2 s  .com*/
    InputStream in = inputStream;
    while ((c = in.read()) != -1) {
        sw.write(c);
    }
    return sw.toString();
}

From source file:Hex.java

/**
 * Encodar binrt till hex//from   w  w w  .  ja  v a2s . com
 *
 *@param dataStr bin-representation av data
 *@return Hex-representation av data
 **/
public static String encode(byte[] dataStr) {
    StringWriter w = new StringWriter();
    for (int i = 0; i < dataStr.length; i++) {
        int b = dataStr[i];
        w.write(hex[((b >> 4) & 0xF)]);
        w.write(hex[((b >> 0) & 0xF)]);
    }
    return w.toString();
}

From source file:de.zib.gndms.GORFX.action.dms.SliceStageInORQCalculator.java

public static String loggalbeStringArray(String[] sl) {

    if (sl.length == 0)
        return "null";

    StringWriter sw = new StringWriter();
    for (String s : sl)
        sw.write(s);

    return sw.toString();
}

From source file:org.sakaiproject.bbb.impl.util.XmlUtil.java

public static String convertPropsToXml(Props props) throws Exception {
    String xml = "";
    StringWriter outputWriter = null;
    try {//from www. jav a  2 s .co  m
        outputWriter = new StringWriter();
        outputWriter.write("<?xml version='1.0' ?>");
        BeanWriter beanWriter = getBeanWriter(outputWriter);
        beanWriter.write("MeetingProperties", props);
        xml = outputWriter.toString();
    } finally {
        outputWriter.close();
    }
    return xml;
}