Friday, March 13, 2015

Java using boolean program



Primitive Boolean constants

package com.java2novice.wrapper.booleanexp;
 public class MyBooleanConstants
 {
     public static void main(String a[])
   {
        System.out.println("Boolean object corresponding to the primitive value false"+Boolean.false);
        System.out.println("Boolean object corresponding to the primitive value true"
        +Boolean.true);
    }
}

Convert string to Boolean value

package com.java2novice.wrapper.booleanexp;
public class MyStringToBoolean
{
    public static void main(String a[])
    {
        String strBol = "true";
        Boolean bol = Boolean.parseBoolean(strBol);
        System.out.println(bol);
    }
}

Create Boolean wrapper object

package com.java2novice.wrapper.booleanexp;
public class MyBasicBoolean
{
     public static void main(String a[])
    {
        boolean b1 = true;
        Boolean bObj1 = new Boolean(b1);
        System.out.println("Wrapper class Boolean output: "+bObj1);
        Boolean bObj2 = new Boolean("false");
        System.out.println("Wrapper class Boolean output: "+bObj2);
        System.out.println(bObj1.booleanValue());
    }
}

Write byte content to a file in java Program

package com.java2novice.files;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ByteWriteToFile
{
    public static void main(String[] args)
    {   
        OutputStream opStream = null;
        try
        {
            String strContent = "This example shows how to write byte content to a file";
            byte[] byteContent = strContent.getBytes();
            File myFile = new File("C:/MyTestFile.txt");
            if (!myFile.exists())
           {
                myFile.createNewFile();
            }
            opStream = new FileOutputStream(myFile);
            opStream.write(byteContent);
            opStream.flush();
            }
           catch (IOException e)
           {
            e.printStackTrace();
           }
             finally
          {
              try
          {
                if(opStream != null) opStream.close();
            }
            catch(Exception ex){}
        }
    }
}

No comments:

Post a Comment