Posts

Showing posts from 2012

File Reading and Writing Example

/*  * Reading and writing powerpoint file  * and Reading and writing text file  * using Java Program  *  */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class TestPackageOutSide {     public static void main(String[] args) {               System.out.println("Out printed....");               try {                       File infile =new File("c:/1.ppt");             FileInputStream fin = new FileInputStream(infile);             FileOutputStream fout = new FileOutputStream("c:/2.ppt");             byte b[] = new byte[(int)infile.length()];             fin.read(b);             fout.write(b);       ...

JApplet Example with FlowLayout and setPreferredSize method

import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JApplet; import javax.swing.JButton; public class JAppletTesting extends JApplet {         FlowLayout fl = new FlowLayout(FlowLayout.LEFT);         JButton btok, btcl;     Dimension d1;     public void init() { // setting layout to FlowLayout               setLayout(fl);         d1 = new Dimension(100, 40);         btok = new JButton("OK");         btcl =new JButton("Close");                 btok.setPreferredSize(d1);         add(btok);         add(btcl);     } }

Garbage Collector Example

class StudentDemo {     int id;     String name;     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public StudentDemo(int id, String name) {         this.id = id;         this.name = name;     }         protected void finalize()     {         System.out.println("object is removed...");     }         } public class TestGarbegeColllector {     public static void main(String[] args) {             StudentDemo ...

Obtaining System fonts and storing in JComboBox example

import java.awt.BorderLayout; import java.awt.GraphicsEnvironment; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class JFrameTesting extends JFrame {     JComboBox fontbox, sizebox;     Integer mysize[] = { 9,10,11,12,14,16,18,20,22,24,30,36,45,60,72};     JPanel p1 = new JPanel();     public JFrameTesting() {         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();         String fontname[] = ge.getAvailableFontFamilyNames();         fontbox = new JComboBox(fontname);         sizebox = new JComboBox(mysize);         sizebox.setSelectedIndex(6);         add(p1,BorderLayout.SOUTH);         p1.add(fontbox);         p1.add(sizebox);                 setSiz...

JFrame with JOptionPane example

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileReader; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class JFrameWithMenu  extends JFrame {     JMenuBar mb ;     JMenu mfile, medit, mhelp;     JMenuItem minew, miopen, misave, miexit, miabout;     JTextArea jta;     JFileChooser fch;     JScrollPane jsp;     public JFrameWithMenu() {                 settingMenu();         jta = new JTextArea();                       jta.setText("Testing text...."); ...

Object Reading writing examples

import java.io.FileInputStream; import java.io.ObjectInputStream; public class ObjectReadingFromFile {     public static void main(String[] args) {         try {                     FileInputStream fin = new FileInputStream("c:/test/student.dat");                 ObjectInputStream objin = new ObjectInputStream(fin);                       StudentData sobj1 = (StudentData)objin.readObject();          StudentData sobj2 = (StudentData)objin.readObject();                  //java.util.Date myd = (java.util.Date)objin.readObject();                     System.out.println("my object creation date and time :");            // System.out.println(myd); ...

HashMap HashSet example

package utilpackageapplication; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; class Student {   int sid;     String sname;     public Student(int sid, String sname) {         this.sid = sid;         this.sname = sname;     }     public int getSid() {         return sid;     }     public void setSid(int sid) {         this.sid = sid;     }     public String getSname() {         return sname;     }     public void setSname(String sname) {         this.sname = sname;     }     } public class HashMap {     public static void main(String[] args) {             Student sd1 = new Student(1,"Udit");             Stu...

Regular Expression Example:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternMatcherTesting {     public static void main(String[] args) {         // TODO code application logic here         Pattern pat = Pattern.compile("[ab]+");             Matcher mat = pat.matcher("subjeaact  caaaode : 63bb0002 Jaaaaava 4a56 PrograbbbJava mming.bbbbb.. Java ba");                 while (mat.find())         {                         System.out.println("Value is :"+ mat.group() + " found at index : " + mat.start());         }         }       }

JFrame example with Menu and JFileChooser and File Opening

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileReader; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextArea; public class JFrameWithMenu  extends JFrame {     JMenuBar mb ;     JMenu mfile, medit, mhelp;     JMenuItem minew, miopen, misave, miexit, miabout;     JTextArea jta;     JFileChooser fch;     public JFrameWithMenu() {               settingMenu();         jta = new JTextArea();               add(jta,BorderLayout.CENTER);         jta.setText("Testing text....");               setSize(400,400);         setVisib...

Wrapper Classes example

public class WrapperTest {     public static void main(String[] args) {           Long l1 =new Long(123214124);       System.out.println(l1.hashCode());         Character c21 = new Character('c');         Integer intvalu = new Integer("123");         Integer intvalue2 = new Integer(1234);         Integer int2 = -128;         Integer int3 = -128;         System.out.println(intvalue2.hashCode());         System.out.println(intvalu.hashCode());         System.out.println(int2.hashCode());         System.out.println(int2==intvalue2);         System.out.println(int2==int3);         Character c1= 97;         Character c2 = 65;         System.out.println(c1=...

Vector Class Example

import java.util.Vector; public class VectorTestClass {     public static void main(String[] args) {         Vector v = new Vector();         v.add(new String("Test"));         v.add(new Integer("123"));         v.add(new Boolean("true"));         v.add(6789);         v.add(new Float(67.78));         System.out.println("Element at index position 1:  "+v.elementAt(1));         // to remove element stored at index 3 ie. 6789             v.remove(3);         for (int i=0; i<v.size(); i++ )         {             System.out.println(v.get(i));         }         //or         /* for (Object a : v.toArray())         ...

Enumeration Example

public enum Apple {         GoldenDel(2),  YellowDel(5), Jonathan(7), GreenDel(12), RedDel(4);         private int price;         Apple(int price)     {         this.price = price;         }         public int getPrice()     {             return price;         }         public void setPrice(int p)     {             this.price = p;         } } //Testing Enumeration Class for enum type public class TestEnum {          public static void main(String[] args) {         Apple ap,ap2=Apple.GoldenDel;         ap=Apple.GoldenDel;         for (Apple ap1 : Apple.values())         {...

Generic Programming Example -2

class GenericDemo2<T, E> {     T obj;     E obj2;     // Pass the constructor a reference to an Object of type T     public GenericDemo2(T obj, E obj2) {         this.obj = obj;         this.obj2= obj2;     }     // return Object     public T getObj() {         return obj;     }     public E getObj2() {         return obj2;     }     void showType()     {         System.out.println("Type of T is :" + obj.getClass().getName());         System.out.println("Type of E is :" + obj2.getClass().getName());     } } public class GenericTypeTesting2 {     public static void main(String[] args) {         GenericDemo2<Integer, String> intojb = new GenericDemo2<Integer,Stri...

Generic Programming example -1

//Here T is name of type parameter // this name is used as a placeholder for the actual type //that will be passed to Gen when an object is created class GenericDemo<T> {     T obj;     // Pass the constructor a reference to an Object of type T     public GenericDemo(T obj) {         this.obj = obj;     }     // return Object     public T getObj() {         return obj;     }     void showType()     {         System.out.println("Type of T is :" + obj.getClass().getName());     }     } public class GenericTypeTesting {     public static void main(String[] args) {         GenericDemo<Integer> intojb = new GenericDemo<Integer>(210);         System.out.println(intojb.getObj());         intojb.showType();...

Content on Linux