Wednesday, November 5, 2014

JAVA BUFFER PROGRAM



Do While loop

public class DoWhileExample
{
public static void main(String[] args)
{
dowhile();
nt i =0;  
do
{
System.out.println("i is : " + i);
i++;
}while(i < 5);
  }
}


Infinite For loop

public class InfiniteForLoop
 {
public static void main(String[] args)
{
   for(;;)
System.out.println("Hello");
  }
}
Java StringBuffer setLength

public class JavaStringBufferSetLengthExample
 {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("StringBuffer setLength method example");
sbf.setLength(12);
System.out.println("StringBuffer contents: " + sbf);
sbf.setLength(0);
System.out.println("StringBuffer contents deleted:" + sbf);
   }
}

Java String Buffer append new line

public class JavaStringBufferAppendNewLineExample
{      
public static void main(String args[])
{
StringBuffer sbf = new StringBuffer("This is the first line.");
sbf.append(System.getProperty("line.separator"));
sbf.append("This is second line.");
System.out.println(sbf);
    }
}

Java String Buffer to InputStream

import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class StringBufferToInputStreamExample
{
public static void main(String args[])
{
 StringBuffer sbf = new StringBuffer("StringBuffer to InputStream Example");
byte[] bytes = sbf.toString().getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
System.out.println("StringBuffer converted to InputStream");
   }
}

String Buffer Reset Java

public class StrinBufferResetExample
{
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Hello World!");
System.out.println("StringBuffer content: " + sbf) ;
sbf.delete(0, sbf.length());
System.out.println("StringBuffer content after reset:" + sbf);
  }
}

String Buffer toString

public class JavaStringBufferToStringExample
{
public static void main(String args[])
{
StringBuffer sbf = new StringBuffer("Hello World!");
String str = sbf.toString();
System.out.println("StringBuffer to String: " + str);
   }
}

String Buffer Trim

public class JavaStringBufferTrimExample
{
public static void main(String[] args)
{
 StringBuffer sbf = new StringBuffer("   Hello World  !  ");
 String str = sbf.toString().trim();
System.out.println("StringBuffer trim: \"" + str +"\"");
System.out.println("\"" + trim(sbf) + "\"");
}
private static String trim(StringBuffer sbf)
{
int start, end;
for(start = 0; start < sbf.length(); start++)
{
if(sbf.charAt(start) != ' ')
break;
}
for(end = sbf.length(); end > start ; end--)
{
if(sbf.charAt(end-1) != ' ')
break;
}
return sbf.substring(start, end);
    }
}
   

No comments:

Post a Comment