Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Sunday, May 26, 2013

Change Background Color In Java

/*Program for background color changing using scrollbar*/

import java.awt.*;
import java.awt.event.*;

class sroll extends Frame implements AdjustmentListener,ActionListener
   {
        Button b;
        Scrollbar sr,sg,sb;
        Label lr,lg,lb;

public sroll()
  {
           b=new Button("EXIT:");
                       lr=new Label("RED:");
          lg=new Label("GREEN:");
          lb=new Label("BLUE:");

                       sr=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sg=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sb=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);

           b.addActionListener(this);
           sr.addAdjustmentListener(this);
          sg.addAdjustmentListener(this);
          sb.addAdjustmentListener(this);

                      setLayout(new FlowLayout());

          add(lr);  add(sr);  add(lg);  add(sg);  add(lb);  add(sb);
add(b);

}


    public void actionPerformed(ActionEvent a1)
      {

System.exit(0);

       }



                 public void adjustmentValueChanged(AdjustmentEvent ae)
      {

 int r=sr.getValue();
 int g=sg.getValue();
 int b=sb.getValue();

Color c=new Color(r,g,b);

   setBackground(c);
   }

public static void main(String args[])
 {


sroll a=new sroll();

a.setSize(200,200);

a.setVisible(true);
}

    }

Convert Checkbox To Radio Button In Java

/*Program to convert checkboxes into radio buttons*/

import java.awt.*;

class elec extends Frame
  {
     Label l1,l2;
     Checkbox a,b,c,d,e,f;
     CheckboxGroup x,y;

        public elec()
{
  setTitle("UNIVERSITY FORM:");
               l1=new Label("OPTION 1:");
  l2=new Label("OPTION 2:");
               x=new CheckboxGroup();
               y=new CheckboxGroup();
              a=new Checkbox("maths",false,x);
              b=new Checkbox("chem",false,x);
              c=new Checkbox("physics",false,x);
              d=new Checkbox("biology",false,y);
              e=new Checkbox("english",false,y);
              f=new Checkbox("hindi",false,y);

          setLayout(new FlowLayout());

          add(l1); add(a);  add(b);  add(c);
          add(l2); add(d);  add(e);  add(f);
  }

    public static void main(String args[])throws Exception
       {
 
           elec r=new elec();

           
            r.setSize(200,300);
            r.setVisible(true);
       }
 
 } 

File Writing In Java

/*Program For File Writing In Java*/

import java.io.*;


class simple
  {
      public static void main(String args[])throws Exception
        {
                File f=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc");
            String fnames[]=f.list();
             

    System.out.println("NAME"+"  "+"LENGTH"+"  "+"STATUS"+"  "+"TYPE");

            for(int i=0;i<fnames.length;i++)
{
  File f1=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc",fnames[i]);
       
               System.out.print(" "+f1.getName());
   System.out.print(" "+f1.length());
if(f1.canRead()==true)
{
  System.out.print(" "+"R");
}
if(f1.canWrite()==true)
 {
   System.out.print(" "+"w");
 }
if(f1.isFile()==true)
{
  System.out.print(" "+"FILE");
}
if(f1.isDirectory()==true)
{
 System.out.print(" "+"DIR");
}
                        System.out.println("");
       }
      }
 }

Number Conversion in Java

/*Program for conversion of a decimal no into binary,octal,hexadecimal*/

import java.string.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;


class dec extends Frame implements ActionListener
 {
     int n,d,a,no,i;
     int ans[]=new int[20];
     String s;
      String d="";
     Button b1,b2,b3,b4,b5,b6;
     TextField t1,t2,t3,t4;
 
         public dec()
              {
        b1=new Button("ENTER NUMBER:");
        b2=new Button("BINARY NUMBER:");
        b3=new Button("OCTAL NUMBER:");
        b4=new Button("HEXADECIMAL NUMBER:");
        b5=new Button("RESET:");
        b6=new Button("EXIT:");
        t1=new TextField(20);
        t2=new TextField(20);
        t3=new TextField(20);
        t4=new TextField(20);

                  setLayout(new FlowLayout());

                  add(b1); add(t1);  add(b2);  add(t2);  add(b3);  add(t3);  add(b4);  add(t4);
      add(b5);  add(b6);

                    b1.addActionListener(this);
       b2.addActionListener(this);
       b3.addActionListener(this);
       b4.addActionListener(this);
       b5.addActionListener(this);
       b6.addActionListener(this);
  }

    public void actionPerformed(ActionEvent ae)
{
 
  if(ae.getSource()==b2)
     {
       s=t1.getText();
         a=Integer.parseInt(s);

                         no=a;                      
                     for(i=1;no!=0;i++,no/=2)
                           ans[i]=no%2;
                       for(i--;i>0;i--)
                            d+=ans[i];  
                       if(t2.getText().equals(""))
                             t2.setText(d);
     }

if(ae.getSource()==b3)
     {
       s=t1.getText();

no=a;
for(d="",no=a,i=1;no!=0;i++,no/=8)  
                        ans[i]=no%8;
                       for(i--;i>0;i--)
                            d+=ans[i];
                         if(t3.getText().equals(""))
                             t3.setText(d);
   }


if(ae.getSource()==b3)
     {
       s=t1.getText();

no=a;
for(d="",no=a,i=1;no!=0;i++,no/=16)
                           ans[i]=no%16;
                     for(i--;i>0;i--)
                          {
                              switch(ans[i])
                                  {
                                       case 10:d+="A";  break;
                                       case 11:d+="B";  break;
                                       case 12:d+="C";  break;
                                       case 13:d+="D";  break;
                                        case 14:d+="E";  break;
                                      case 15:d+="F";  break;
                                      default:d+=ans[i];
                                  }
                        }
                              if(t4.getText().equals(""))
                                    t4.setText(d);
                  }

if(ae.getSource()==b5)
     {
            t1.setText("");
     }

if(ae.getSource()==b6)
     {

System.exit(0);
     }
}

      public static void main(String args[])throws Exception
{
    dec de=new dec();

     de.setSize(200,400);
     de.setVisible(true);
}
    }




Pattern in Java

/**Program For Design pattern in java**/

import java.io.*;

class loop
  {
       int i,j,n;

      public void getdata()throws Exception
         {
             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               System.out.println("ENTER THE NUMBER OF LINES TO BE PRINTED:");
                 n=Integer.parseInt(br.readLine());
         }
     public void display()
         {
              for(i=0;i<n;i++)
                {
                  for(j=i;j<n;j++)
                    {
                        System.out.print("*");
                    }
                        System.out.println(" ");        
                 }
                           
                   
          }
                   
             
        public static void main(String args[])throws Exception
         {
            loop l=new loop();
   
             l.getdata();
             l.display();
         }
  }
 

Graphics in Java

/**Graphics In Java Example **/

import java.awt.*;
import java.awt.event.*;

class line extends Frame implements MouseListener,ActionListener
 {

     int x1,x2,y1,y2;
     Button b;
   
public line()
 {
 
    addMouseListener(this);
                 b=new Button("EXIT");

     setLayout(new FlowLayout());
                    add(b);
                       b.addActionListener(this);
 }

public void actionPerformed(ActionEvent ae)
 {

     System.exit(0);
 }


public void mousePressed(MouseEvent me)
 {
    x1=me.getX();
    y1=me.getY();
 }

public void mouseReleased(MouseEvent me)
 {
    x2=me.getX();
    y2=me.getY();
 
    Graphics g=getGraphics();

   g.drawLine(x1,y1,x2,y2);
}

public void mouseClicked(MouseEvent me)
 {
 }

public void mouseEntered(MouseEvent me)
 {
 }

public void mouseExited(MouseEvent me)
 {
 }

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

     line l=new line();

    l.setSize(600,600);
    l.setVisible(true);
}

    }

Inheritance in Java

/**Program For Inheritance in Java**/


import java.lang.*;

class xyz
  {
       int x;
     
public void getdata()
  {
    x=5;
  }
public void putdata()
 {
   System.out.println(x);
 }
  }

class abc extends xyz
  {
       int a;

public void getdata()
{
  a=7;
}
public void putdata()
{
  System.out.println(a);
}

public static void main(String args[])throws Exception
    {  
abc a=new abc();
xyz x=new xyz();

         

x.getdata();
a.getdata();
x.putdata();
a.putdata();
 }
  }

Frame in Java

/**PROGRAM FOR FRAME (WINDOW)**/

import java.lang.*;
import java.awt.*;

class demo extends Frame
  {
   

     public static void main(String args[])throws Exception
          {
                 demo d=new demo();

    d.setSize(1600,800);

               d.setVisible(true);
          }
  }

                 

Editor in Java

/**EDITOR PROGRAM IN JAVA**/


import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;


public class editor extends Frame implements ActionListener
   {
      MenuBar mb;
      Menu m1,m2,m3,m4,m5;
      MenuItem a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,c1,c2,d1,e1,e2;
      TextArea t;

        editor()
          {
            t=new TextArea();
            mb=new MenuBar();
            m1=new Menu("FILE");
            a1=new MenuItem("NEW");
a2=new MenuItem("OPEN");
a3=new MenuItem("SAVE");
a4=new MenuItem("SAVE AS");
a5=new MenuItem("PRINT");
a6=new MenuItem("CLOSE");

         a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);

         

m2=new Menu("EDIT");
b1=new MenuItem("UNDO");
b2=new MenuItem("CUT");
b3=new MenuItem("COPY");
b4=new MenuItem("PASTE");
b5=new MenuItem("SELECT");
b6=new MenuItem("TIME");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);

m3=new Menu("FORMAT");
c1=new MenuItem("WORDWRAP");
c2=new MenuItem("FONT");

c1.addActionListener(this);
c2.addActionListener(this);

m4=new Menu("VIEW");
d1=new MenuItem("STATUS BAR");

d1.addActionListener(this);

m5=new Menu("HELP");
e1=new MenuItem("HELP TOPICS");
e2=new MenuItem("ABOUT NOTEPAD");

e1.addActionListener(this);
e2.addActionListener(this);

             setLayout(new GridLayout(1,1));

           add(t);
           mb.add(m1); mb.add(m2);  mb.add(m3); mb.add(m4);  mb.add(m5);
           m1.add(a1); m1.add(a2); m1.add(a3); m1.add(a4); m1.add(a5);m1.add(a6);

          m2.add(b1); m2.add(b2); m2.add(b3); m2.add(b4); m2.add(b5);m2.add(b6);

          m3.add(c1); m3.add(c2);

          m4.add(d1);  m5.add(e1);  m5.add(e2);

       

setMenuBar(mb);  
       
        }

    public void actionPerformed(ActionEvent ae)
            {
  if(ae.getSource()==a6)
{
   System.exit(0);
}


         }




public static void main(String args[])
  {
    editor h= new editor();
    h.setTitle("Notepad");
    h.setVisible(true);
    h.setSize(500,500);
  }

         
    }

         


Mouse handling in Java

/**PROGRAM OF MOUSE HANDLING**/

import java.lang.*;
import java.awt.*;
import java.awt.event.*;


class date1 extends Frame implements ActionListener,AdjustmentListener
  {

     Button a,b;
     Label l1,l2,l3,l4;
     TextField  t1,t2,t3,t4;
     Scrollbar s1,s2,s3;
     String g1,g2,g3;
      int da,mo,ye;
       int z,x,y;
               

     public date1()
{
  a=new Button("OK");
  b=new Button("EXIT");
  l1=new Label("DD");
  l2=new Label("MM");
  l3=new Label("YY");
  l4=new Label("DATE");

 t1=new TextField(5);
 t2=new TextField(10);
 t3=new TextField(5);
 t4=new TextField(20);

 s1=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,31);
 s2=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,12);
 s3=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1900,2100);

            setLayout(new FlowLayout());

              add(l1);  add(s1);  add(t1);  add(l2);  add(s2);  add(t2);
  add(l3);  add(s3);  add(t3);
               add(a);  add(l4);  add(t4);  add(b);

 a.addActionListener(this);
 b.addActionListener(this);

 s1.addAdjustmentListener(this);
 s2.addAdjustmentListener(this);
 s3.addAdjustmentListener(this);
         
          }

      public void actionPerformed(ActionEvent ae)
           {
   
    if(ae.getSource()==a)
                  {
                   
          g1=t1.getText();
         
         
         g2=t2.getText();  

        g3=t3.getText();
       
                     t4.setText(g1+"/"+g2+"/"+g3);
     }

  if(ae.getSource()==b)
     {
        System.exit(0);
     }
}

         public void adjustmentValueChanged(AdjustmentEvent be)
{  
 
     da=s1.getValue();
      t1.setText(" "+da);

    mo=s2.getValue();
    t2.setText(" "+mo);

     ye=s3.getValue();
     t3.setText(" "+ye);

}

public static void main(String args[])throws Exception
 {
                 date1 q=new date1();

    q.setSize(400,400);
    q.setVisible(true);
}

      }
         



Date In Java

/***PROGRAM FOR DATE*/

import java.io.*;

class date
{
   int dd,mm,yy;

public void getdate()throws Exception
{
  int flag=0;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 
  while(flag==0)
   {
                   
       try
{
   System.out.println("ENTER THE DATE:");
     dd=Integer.parseInt(br.readLine());
     mm=Integer.parseInt(br.readLine());
     yy=Integer.parseInt(br.readLine());

flag=1;
}

catch(NumberFormatException ne)
  {
    System.out.println("Exception occured: INVALID NUMBER" +ne);
  }
                  };
               }

    public void putdate()
{
    System.out.println(dd+"/"+mm+"/"+yy);
}
 
           public static void main(String args[])throws Exception
       {
                      date d=new date();

                          d.getdate();
  d.putdate();
                  }
  }

Calculator in Java

/**JAVA PROGRAM FOR CALCULATOR**/


import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class calc extends Frame implements ActionListener
   {
        int val1,val2,res,flag;
            TextField t1;
          Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;

           public calc()
            {
               b0=new Button("0");
               b1=new Button("1");
   b2=new Button("2");
  b3=new Button("3");
 b4=new Button("4");
  b5=new Button("5");
  b6=new Button("6");
  b7=new Button("7");
  b8=new Button("8");
  b9=new Button("9");
  b10=new Button("+");
  b11=new Button("-");
  b12=new Button("*");
  b13=new Button("/");
  b14=new Button(".");
  b15=new Button("=");
  b16=new Button("OFF");
  b17=new Button("CLEAR");

  t1=new TextField(5);

setLayout(new FlowLayout());
       
           add(t1); add(b17); add(b16); add(b1); add(b2);  add(b3);  add(b10); add(b4); add(b5); add(b6);
add(b11);  add(b7);  add(b8); add(b9);  add(b12);  add(b0); add(b14);  add(b15); add(b13);

  b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);

        }

       public void actionPerformed(ActionEvent ae)
{
 
   if(ae.getSource()==b0)
       {
       t1.setText(t1.getText()+"0");
     }

  if(ae.getSource()==b1)
       {
       t1.setText(t1.getText()+"1");
     }

if(ae.getSource()==b2)
       {
       t1.setText(t1.getText()+"2");
     }

if(ae.getSource()==b3)
       {
       t1.setText(t1.getText()+"3");
     }

if(ae.getSource()==b4)
       {
       t1.setText(t1.getText()+"4");
     }

if(ae.getSource()==b5)
       {
       t1.setText(t1.getText()+"5");
     }

if(ae.getSource()==b6)
       {
       t1.setText(t1.getText()+"6");
     }

if(ae.getSource()==b7)
       {
       t1.setText(t1.getText()+"7");
     }

if(ae.getSource()==b8)
       {
       t1.setText(t1.getText()+"8");
     }

if(ae.getSource()==b9)
       {
       t1.setText(t1.getText()+"9");
     }

if(ae.getSource()==b10)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=1;
     }



if(ae.getSource()==b11)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=2;
     }



if(ae.getSource()==b12)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=3;
     }



if(ae.getSource()==b13)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText("");
          flag=4;
     }



if(ae.getSource()==b17)
       {
                      t1.setText("");    
     }



if(ae.getSource()==b16)
       {
       
        System.exit(0);      
    }

         

if(ae.getSource()==b14)
       {
         val1=Integer.parseInt(t1.getText());
                      t1.setText(val1+".");

                     
     }



if(ae.getSource()==b15)
       {
                      val2=Integer.parseInt(t1.getText());
   

         switch(flag)
              {
   
      case 1: res=val1+val2;
break;
      case 2: res=val1-val2;
break;

      case 3: res=val1*val2;
break;

      case 4: res=val1/val2;
break;
}

  t1.setText(""+res);
                }

         }

                 public static void main(String args[])
{

    calc y=new calc();

     y.setSize(100,200);
     y.setVisible(true);
                       }
           }

Action Listener

/**ACTION LISTENER IN JAVA EXAMPLE**/

 import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class bb extends Frame implements ActionListener
 {
 
    Button b;
    TextField t;

      public bb()
{
   b=new Button("CLICK:");
                t=new TextField(20);

          setLayout(new FlowLayout());

             add(b);    add(t);

              b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent ae)
               {
                   if(ae.getSource()==b)
                      {
                          Frame f=new Frame();
                            {
                                f.setSize(50,50);
                                f.setVisible(true);
                           }
                      }
               }

          public static void main(String args[])
            {
               
                 bb a=new bb();
         
                    a.setSize(400,600);
                    a.setVisible(true);

             }
     }


             

Addition Of Numbers

/*Example of class for addition of the two numbers*/

import java.awt.*;
import java.awt.event.*;
class as extends Frame implements ActionListener
  {
        TextField t1,t2,t3;    
         Button b1,b2,b3;

            public as()
              {
                     t1=new TextField(10);
         t2=new TextField(10);
                     t3=new TextField(10);
                      b1=new Button("ADD");
          b2=new Button("SUB");
          b3=new Button("EXIT");

              setLayout(new FlowLayout());
             
              add(b1); add(t1); add(b2);       add(t2);  add(t3);   add(b3);
             
                b1.addActionListener(this);
    b2.addActionListener(this);
                b3.addActionListener(this);
 
          }

            public static void main(String args[])throws Exception
   {
   
      as a=new as();

                  a.setSize(200,300);
                   a.setVisible(true);
             }

        public void actionPerformed(ActionEvent ae)
  {
 
                 int val1=Integer.parseInt(t1.getText());
     int val2=Integer.parseInt(t2.getText());
     int res;

if(ae.getSource()==b1)
   {
                    res=val1+val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b2)
   {
                    res=val1-val2;
                     t3.setText(" "+res);
               }

           
if(ae.getSource()==b3)
   {
                    System.exit(0);
               }
      }
  }
    

Simple Applet Program

Following is a Simple applet program inn java.



import java.awt.*;
import java.applet.*;

public class app2 extends Applet
  {
     public void paint(Graphics g)
       {
             g.drawString("MY FIRST APPLET",30,30);
       }
  }

Applet in Java

HTML FILE CODE:

Create a file name sample.html and write the following code in it.

<applet code="app1"width=600 height=600></applet>


Then create a java file and write the following code in it.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class app1 extends Applet implements ActionListener
 {
   
    Button b;
    TextField t;

      public void Init()
{
   b=new Button("CLICK:");
                t=new TextField(20);

          setLayout(new FlowLayout());

             add(b);    add(t);

              b.addActionListener(this);
         }

         public void actionPerformed(ActionEvent ae)
               {
                   if(ae.getSource()==b)
                      {
                          t.setText("YOUR NAME HERE:");
                      }
               }

         
     }

 
              

Simple Event Program In Java

/*EVENT PROGRAM IN JAVA*/

import java.lang.*;
import java.awt.*;
import java.awt.event.*;

class aaa extends Frame implements ActionListener
 {

     Button b1,b2,b3,b4,b5,b6;
     Label a,b,c,d,r,t;
     TextField a1,a2,a3,a4,a5,a6,a7;
       int a1;

           public aaa()
{
   b1=new Button("SUBMIT:");
   b2=new Button("RESET:");
   b3=new Button("EXIT:");
   b4=new Button("TOTAL:");
   b5=new Button("NEW:");
   b6=new Button("OLD:");

   a=new Label("NAME:");
   b=new Label("AGE:");
   c=new Label("GENDER:");
   d=new Label("EMAIL_ID:");
   r=new Label("MOBILE NUMBER:");
   t=new Label("NATIONALITY:");

   a1=new TextField(20);
   a2=new TextField(20);
   a3=new TextField(20);
   a4=new TextField(20);
   a5=new TextField(20);
   a6=new TextField(20);
   a7=new TextField(20);

    setLayout(new FlowLayout());
                  add(b5);  add(b6);
   
                   

                b1.addActionListener(this);
                b2.addActionListener(this);
   b3.addActionListener(this);
   b4.addActionListener(this);
   b5.addActionListener(this);
   b6.addActionListener(this);

          }

public aaa(int a1)
{
                 if(a1==0)
          {
            setLayout(new FlowLayout());
                           add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);
          }
 }

          public void actionPerformed(ActionEvent ae)
{
  if(ae.getSource()==b5)
     {
        aaa(0);
     }

   if(ae.getSource()==b6)
      {
       
      setLayout(new FlowLayout());
            add(a);  add(b);  add(c);  add(d);  add(r);  add(t);
               add(b1);  add(b2);  add(b3);  add(b4);
       }
             
                  if(ae.getSource()==b3)
       {
                         System.exit(0);
       }
             

            }

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

aaa g=new aaa();

                       g.setSize(300,600);

          g.setVisible(true);

             }

      }

       
 

Monday, October 22, 2012

Search Pattern in Java

Program to find a specific pattern from a given text or file is stated as below.



import java.io.*;

class srchPattern
{
  public static void main(String args[])
  {
    if(args.length == 0 || args.length == 1)
    {
      System.out.println("Insufficient arguments...");
      return;
    }

    File f = new File(args[1]);

    try
    {
      BufferedReader i = new BufferedReader(new FileReader(args[1]));

      String line;
      int ct = 0;

      if(f.exists() && f.isFile())
      {
        while((line = i.readLine()) != null)
        {
          if((line.indexOf(args[0])) != -1)
          {
            ct++;
          }
          System.out.println(line + " ");
        }

        if(ct == 0)
        {
          System.out.println("Pattern not Found");
        }
        else
        {
          System.out.println("Pattern found in the file...");
          System.out.println("Number of Occurances of the word " + args[0] + " in te file " + args[1] + " is " + ct);
        }
      }
    }
    catch(Exception e){}
  }
}

Word,character count in Java

Program  to count the number of occurrences of a word in a file is given as below:


import java.io.*;

class wordcnt
{
  public static void main(String args[])
  {
    int i = 0,cnt = 0;
    try
    {
      FileInputStream fin = new FileInputStream("d:/java/wordcnt.java");
      String word;
      char ch , charr[];
      charr = new char[100];
      String w = new String("word");
      while(( ch = (char)fin.read()) != -1)
      {
        if( ch != ' ' && ch != '\n')
        {
           charr[i] = ch ;
           i++;
        }
        else
        {
           word = new String(charr,0,i);
           if(word.equals(w))
              cnt++;   
           if( ch == '\n')
              System.out.println( word );
           else
              System.out.print( word + " ");
           i = 0;
        }
     };
     fin.close();
    }
    catch(Exception ie)
    {
    }
    System.out.println("\n\nWord Count : " + cnt);
  }
}

Copy file in Java

Program to copy a file content in java is as follows:

 import java.io.*;

class FileCopy
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fin = new FileInputStream("d:/java/stk.java");
    FileOutputStream fout = new FileOutputStream("d:/java/txtfile.txt");
    int b;

    while(true)
    {
      b = fin.read();
      if(b == -1)
      {
        break;
      }
      System.out.print((char)b);
      fout.write(b);
    }

    fin.close();
    fout.close();
  }
}

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...