Example usage for java.lang StringBuffer append

List of usage examples for java.lang StringBuffer append

Introduction

In this page you can find the example usage for java.lang StringBuffer append.

Prototype

@Override
    public synchronized StringBuffer append(double d) 

Source Link

Usage

From source file:StringBufferDemo.java

public static void main(String[] argv) {
    String s1 = "Hello" + ", " + "World";
    System.out.println(s1);//from  ww w .j  av a  2  s  .co  m

    // Build a StringBuffer, and append some things to it.
    StringBuffer sb2 = new StringBuffer();
    sb2.append("Hello");
    sb2.append(',');
    sb2.append(' ');
    sb2.append("World");

    // Get the StringBuffer's value as a String, and print it.
    String s2 = sb2.toString();
    System.out.println(s2);

    // Now do the above all over again, but in a more 
    // concise (and typical "real-world" Java) fashion.

    StringBuffer sb3 = new StringBuffer().append("Hello").append(',').append(' ').append("World");
    System.out.println(sb3.toString());

    // Exercise for the reader: do it all AGAIN but without
    // creating any temporary variables.
}

From source file:StringIOApp.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from  ww w  .j ava  2s  .  c o  m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
    StringReader inStream;
    inStream = new StringReader(outStream.toString());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    CharArrayWriter outStream = new CharArrayWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));// w w  w  . j a  v a  2  s.c om
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    CharArrayReader inStream;
    inStream = new CharArrayReader(outStream.toCharArray());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8080);
    ss.setNeedClientAuth(true);/*w w  w  .  ja va2  s . c o m*/

    while (true) {
        try {
            Socket s = ss.accept();
            OutputStream out = s.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = null;
            while (((line = in.readLine()) != null) && (!("".equals(line)))) {
                System.out.println(line);
            }
            System.out.println("");

            StringBuffer buffer = new StringBuffer();
            buffer.append("<HTML>\n");
            buffer.append("<HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n");
            buffer.append("<BODY>\n");
            buffer.append("<H1>Success!</H1>\n");
            buffer.append("</BODY>\n");
            buffer.append("</HTML>\n");

            String string = buffer.toString();
            byte[] data = string.getBytes();
            out.write("HTTP/1.0 200 OK\n".getBytes());
            out.write(new String("Content-Length: " + data.length + "\n").getBytes());
            out.write("Content-Type: text/html\n\n".getBytes());
            out.write(data);
            out.flush();

            out.close();
            in.close();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    String hostname = "tock.usno.navy.mil";

    Socket theSocket = new Socket(hostname, 13);
    InputStream timeStream = theSocket.getInputStream();
    StringBuffer time = new StringBuffer();
    int c;//w  ww . j a va 2  s  .  co  m
    while ((c = timeStream.read()) != -1)
        time.append((char) c);
    String timeString = time.toString().trim();
    System.out.println("It is " + timeString + " at " + hostname);
}

From source file:HTTPServer.java

public static void main(String[] args) throws Exception {

    ServerSocket sSocket = new ServerSocket(1777);
    while (true) {
        System.out.println("Waiting for a client...");
        Socket newSocket = sSocket.accept();
        System.out.println("accepted the socket");

        OutputStream os = newSocket.getOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));

        String inLine = null;//ww  w .  ja  va  2  s. c om
        while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) {
            System.out.println(inLine);
        }
        System.out.println("");

        StringBuffer sb = new StringBuffer();
        sb.append("<html>\n");
        sb.append("<head>\n");
        sb.append("<title>Java \n");
        sb.append("</title>\n");
        sb.append("</head>\n");
        sb.append("<body>\n");
        sb.append("<H1>HTTPServer Works!</H1>\n");
        sb.append("</body>\n");
        sb.append("</html>\n");

        String string = sb.toString();

        byte[] byteArray = string.getBytes();

        os.write("HTTP/1.0 200 OK\n".getBytes());
        os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes());
        os.write("Content-Type: text/html\n\n".getBytes());

        os.write(byteArray);
        os.flush();

        os.close();
        br.close();
        newSocket.close();
    }

}

From source file:StringBufferCommaList.java

public static void main(String[] args) {
    StringTokenizer st = new StringTokenizer("Alpha Bravo Charlie");
    StringBuffer sb = new StringBuffer();
    while (st.hasMoreElements()) {
        sb.append(st.nextToken());
        if (st.hasMoreElements()) {
            sb.append(", ");
        }// w ww  .  j  av  a 2  s.c o  m
    }
    System.out.println(sb);
}

From source file:Main.java

public static void main(String[] arg) {

    StringBuffer buffer = new StringBuffer();
    char[] chars = new char[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };
    buffer.append(chars);
    System.out.println(buffer);/*from  w ww . java 2  s .  co m*/
}

From source file:Main.java

public static void main(String... args) throws IOException {
    URL url = new URL("http://java2s.com");
    InputStream is = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuffer sb = new StringBuffer();

    String line;//from  ww w .  j a va 2 s .  c  om

    while ((line = br.readLine()) != null)
        sb.append(line + System.lineSeparator());

    br.close();

    System.out.print(sb.toString());
}

From source file:HelloPeopleJDOM.java

public static void main(String[] args) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build("./src/data.xml");

    StringBuffer output = new StringBuffer();

    // create the basic HTML output
    output.append("<html>\n").append("<head>\n<title>\nPerson List</title>\n</head>\n").append("<body>\n")
            .append("<ul>\n");

    Iterator itr = doc.getRootElement().getChildren().iterator();

    while (itr.hasNext()) {
        Element elem = (Element) itr.next();

        output.append("<li>");
        output.append(elem.getAttribute("lastName").getValue());
        output.append(", ");
        output.append(elem.getAttribute("firstName").getValue());
        output.append("</li>\n");
    }/*from w  w w.j  a  va  2s.  co m*/

    // create the end of the HTML output
    output.append("</ul>\n</body>\n</html>");

    System.out.println(output.toString());
}