Skip to main content

Posts

Showing posts from March 15, 2012

How to create a zip file in java

  import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * * @author dell-pc */ public class Main { /** * Creates a zip file */ public void createZipFile() { try { String inputFileName = " test.txt "; String zipFileName = " compressed.zip "; //Create input and output streams FileInputStream inStream = new FileInputStream(inputFileName); ZipOutputStream outStream = new ZipOutputStream( new FileOutputStream(zipFileName)); // Add a zip entry to the output stream outStream.putNextEntry( new ZipEntry(inputFileName)); byte [] buffer = new byte [1024]; int bytesRead; //Each chunk of data read from the input stream //is written to the o

Java Swing class hierarchy

import javax.swing.JFrame; import javax.swing.JLabel; //import statements //Check if window closes automatically. Otherwise add suitable code public class HelloWorldFrame extends JFrame { public static void main(String args[]) { new HelloWorldFrame(); } HelloWorldFrame() { JLabel jlbHelloWorld = new JLabel(" Hello World "); add(jlbHelloWorld); this .setSize(100, 100); // pack(); setVisible( true ); } }

Session Management in ASP.NET

State management is a process by which the state and information of a page can be maintained over multiple requests. Asp.net provides several techniques for state management. State Management is basically divided into two parts: 1. Client side state management 2. Server side state management Client side state management The state of the page is maintained at the client side. Following are the various techniques for this: a. ViewState b. Cookies c. QueryString Server side state management The state of the page is maintained at the server side.Following are the various techniques for this: a. Application Variables b. Session Variables c. Sql Server I have attached the design page image which is same for every page for the sake of convenience. ViewState View State can be used to store state information for a single user. View State is a built in feature in web controls to persist data between page post backs. You can set View State on/off for each control using EnableViewState property.

Encapsulation example in c#

1. BankAccountExternal.cs using System; public class BankAccountExternal { public decimal GetAmount() { return 2000.00m; } } 2. BankAccountPrivate.cs using System; class BankAccountPrivate { private string m_name; public string CustomerName { get { return m_name; } set { m_name = value ; } } } 3. BankAccountProtected.cs using System; class BankAccountProtected { public void CloseAccount() { ApplyPenalties(); CalculateFinalInterest(); DeleteAccountFromDB(); } protected virtual void ApplyPenalties() { // deduct from account } protected virtual void CalculateFinalInterest() { // add to account } protected virtual void DeleteAccountFromDB() { // send notification to data entry personnel } } 4. BankAccountPublic.cs using System; class BankAccountPublic { public decimal GetAmount() { return 1000.00

Introduction to Enums

using System; // declares the enum public enum Volume : byte { Low = 1, Medium, High } class EnumBaseAndMembers { static void Main() { // create and initialize // instance of enum type Volume myVolume = Volume.Low; // make decision based // on enum value switch (myVolume) { case Volume.Low: Console.WriteLine(" The volume has been turned Down. "); break ; case Volume.Medium: Console.WriteLine(" The volume is in the middle. "); break ; case Volume.High: Console.WriteLine(" The volume has been turned up. "); break ; } Console.ReadLine(); } }

Introduction to Exception Handling

using System; using System.IO; class ExceptionDemo { static void Main( string [] args) { FileStream outStream = null ; FileStream inStream = null ; try { outStream = File.OpenWrite(" DestinationFile.txt "); inStream = File.OpenRead(" BogusInputFile.txt "); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { if (outStream != null ) { outStream.Close(); Console.WriteLine(" outStream closed. "); } if (inStream != null ) { inStream.Close(); Console.WriteLine(" inStream closed. "); } } } }

Introduction to Delegates

using System; // this is the delegate declaration public delegate int Comparer( object obj1, object obj2); public class Name { public string FirstName = null ; public string LastName = null ; public Name( string first, string last) { FirstName = first; LastName = last; } // this is the delegate method handler public static int CompareFirstNames( object name1, object name2) { string n1 = ((Name)name1).FirstName; string n2 = ((Name)name2).FirstName; if (String.Compare(n1, n2) > 0) { return 1; } else if (String.Compare(n1, n2) < 0) { return -1; } else { return 0; } } public override string ToString() { return FirstName + " " + LastName; } } class SimpleDelegate { Name[] names = new Name[5]; public SimpleDelegate() { names[0]

Introduction to Events

using System; using System.Drawing; using System.Windows.Forms; // custom delegate public delegate void StartDelegate(); class EventDemo : Form { // custom event public event StartDelegate StartEvent; public EventDemo() { Button clickMe = new Button(); clickMe.Parent = this ; clickMe.Text = " Click Me "; clickMe.Location = new Point((ClientSize.Width - clickMe.Width) /2,(ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "StartDelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new StartDelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked( object se