Friday, 3 June 2011

Linked list Example in java


import java.util.*;

public class LinkedListDemo{
public static void main(String[] args){
LinkedList link=new LinkedList();
link.add("a");
link.add("b");
link.add(new Integer(10));
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.addFirst(new Integer(20));
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.addLast("c");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.add(2,"j");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.add(1,"t");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.remove(3);
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());
}
}

copying a file to destination and showing details using java



import java.io.*;
import java.util.*;

public class CopyFile {

    public static void copy(String source, String destination)
            throws IOException {
        File source_file = new File(source);
        File desti_file = new File(destination);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] buffer;
        int byte_read;

        try {

            /* first check that file exists or not. */
            if (!source_file.exists() || !source_file.isFile()) {
                throw new ClassException("No source file found : "+source);
            }

/* check that the file is readable or not. */
            if (!source_file.canRead()) {
                throw new ClassException("Source file is unreadable: "+source);
            }
           
            /* If the destination exists, make sure it is a writeable file and ask before
             overwriting it. If the destination doesn't exist, make sure the directory
             exists and is writeable.*/
            if (desti_file.exists()) {
                if (desti_file.isFile()) {
                    DataInputStream in = new DataInputStream(System.in);

                    if (!desti_file.canWrite()) {
                        throw new ClassException("Destination is unwriteable : "+destination);
                    }
                    System.out.print("File " + destination + " already exists. Overwrite?(Y/N): ");
                    System.out.flush();
                    String response = in.readLine();
                    if (!response.equals("Y") && !response.equals("y")) {
                        throw new ClassException("Wrong Input.");
                    }
                }
   else {
                   throw new ClassException("Destination is not a normal file: " + destination);
                }
            }
else {
                File parentdir = parent(desti_file);
                if (!parentdir.exists()) {
                    throw new ClassException("No Destination directory exist: " + destination);
                }
                if (!parentdir.canWrite()) {
                    throw new ClassException("Destination directory is unwriteable: "
                            + destination);
                }
            }

            /* Now we have checked all the things so we can copy the file now.*/
            fis = new FileInputStream(source_file);
            fos = new FileOutputStream(desti_file);
            buffer = new byte[1024];
            while (true) {
                byte_read = fis.read(buffer);
                if (byte_read == -1) {
                    break;
                }
                fos.write(buffer, 0, byte_read);
            }
        } /* Finally close the stream. */
finally {
                           
                fis.close();
fos.close();            
        }
System.out.print("Last modification date of destination file : ");
        System.out.println(new Date(desti_file.lastModified()));
    }

    /* File.getParent() can return null when the file is specified without a directory or
is in the root directory. This method handles those cases.*/
    private static File parent(File f) {
        String dirname = f.getParent();
        if (dirname == null) {
            if (f.isAbsolute()) {
                return new File(File.separator);
            }
else {
                return new File(System.getProperty("user.dir"));
            }
        }
        return new File(dirname);
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            System.err.println("Wrong argument entered. Try Again......");
        }
else {
            try {
                copy(args[0], args[1]);
            }
catch (IOException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
}

class ClassException extends IOException {

    public ClassException(String msg) {
        super(msg);
    }
}




Uncompressing GZip file using java


import java.util.zip.GZIPInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class JavaUncompress{
public static void main(String args[]){
try{
//To Uncompress GZip File Contents we need to open the gzip file.....
if(args.length<=0){
System.out.println("Please enter the valid file name");
}
else{
String inFilename = args[0];
System.out.println("Opening the gzip file.......................... :  opened");
GZIPInputStream gzipInputStream = null;
FileInputStream fileInputStream = null;
gzipInputStream = new GZIPInputStream(new FileInputStream(inFilename));
System.out.println("Opening the output file............. : opened");
String outFilename = inFilename +".pdf";
OutputStream out = new FileOutputStream(outFilename);
System.out.println("Trsansferring bytes from the compressed file to the output file........:  Transfer successful");
byte[] buf = new byte[1024];  //size can be changed according to programmer's need.
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
System.out.println("The file and stream is ......closing.......... : closed");
gzipInputStream.close();
out.close();
}
}
catch(IOException e){
System.out.println("Exception has been thrown" + e);
}
}
}


compressing a file in to GZIP


import java.io.*;
import java.util.zip.*;

public class CompressingFile {
public static void doCompressFile(String inFileName){
try{
File file = new File(inFileName);
System.out.println(" you are going to gzip the  : " + file + "file");
FileOutputStream fos = new FileOutputStream(file + ".gz");
System.out.println(" Now the name of this gzip file is  : " + file + ".gz" );
GZIPOutputStream gzos = new GZIPOutputStream(fos);
System.out.println(" opening the input stream");
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
System.out.println("Transferring file from" + inFileName + " to " + file + ".gz");
byte[] buffer = new byte[1024];
int i;
while ((i = in.read(buffer)) >= 0){
gzos.write(buffer,0,i);
}
System.out.println(" file is in now gzip format");
in.close();
gzos.close();
}
catch(IOException e){
System.out.println("Exception is" + e);
}
}
public static void main(String args[]){
if(args.length!=1){
System.err.println("Please enter the file name which needs to be compressed ");
}
else{
doCompressFile(args[0]);
}
}
}


Constructor Overloading in java


public class ConstructorOverloading{
public static void main(String args[]){
Rectangle rectangle1=new Rectangle(2,4);
int areaInFirstConstructor=rectangle1.first();
System.out.println(" The area of a rectangle in first constructor is :  " + areaInFirstConstructor);
Rectangle rectangle2=new Rectangle(5);
int areaInSecondConstructor=rectangle2.second();
System.out.println(" The area of a rectangle in first constructor is :  " + areaInSecondConstructor);
Rectangle rectangle3=new Rectangle(2.0f);
float areaInThirdConstructor=rectangle3.third();
System.out.println(" The area of a rectangle in first constructor is :  " + areaInThirdConstructor);
Rectangle rectangle4=new Rectangle(3.0f,2.0f);
float areaInFourthConstructor=rectangle4.fourth();
System.out.println(" The area of a rectangle in first constructor is :  " + areaInFourthConstructor);
}
}

class Rectangle{
int l, b;
float p, q;
public Rectangle(int x, int y){
l = x;
b = y;
}
public int first(){
return(l * b);
}
public Rectangle(int x){
l = x;
b = x;
}
public int second(){
return(l * b);
}
public Rectangle(float x){
p = x;
q = x;
}
public float third(){
return(p * q);
}
public Rectangle(float x, float y){
p = x;
q = y;
}
public float fourth(){
return(p * q);
}
}


constructor in java


class another{
int x,y;
another(int a, int b){
x = a;
y = b;
}
another(){
}
int area(){
int ar = x*y;
return(ar);
}
}
public class Construct{
   public static void main(String[] args)
   {
  another b = new another();
  b.x = 2;
  b.y = 3;
  System.out.println("Area of rectangle : " + b.area());
  System.out.println("Value of y in another class : " + b.y);
  another a = new another(1,1);
  System.out.println("Area of rectangle : " + a.area());
  System.out.println("Value of x in another class : " + a.x);
   }
}

Output:
--------------
Area of rectangle: 6
Value of y in another class: 3
Area of rectangle: 1
Value of x in another class: 1