Pages

Monday, 7 March 2011

SEM1 java ass 5

****************************Ass:5.Set:A.Que:1*********************************
Que)       Write a program to accept a string as command line argument and check whether it
is a file or directory. If it is a directory, list the contents of the directory, count how many
files the directory has and delete all files in that directory having extension .txt. (Ask the
user if the files have to be deleted). If it is a file, display all information about the file
(path, size, attributes etc).



*********************************Java Code***********************************

import java.io.*;

class DirDemo
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String dirname=args[0],ext;
        int ch,i,cnt=0;
        File f1=new File(dirname);
        ext="txt";
        if(f1.isFile())
        {
            System.out.println(f1+" is a File\n");
            System.out.println("Path      : "+f1.getPath());
            System.out.println("File Size : "+f1.length()+" bytes\n");
        }

        else if(f1.isDirectory())
        {
            System.out.println(args[0]+" Is a Directory\n");
            System.out.println("Contents Of : "+dirname);
            String s[]=f1.list();

            for(i=0;i<s.length;i++)
            {
                File f=new File(dirname,s[i]);
                if(f.isFile())
                {   
                    cnt++;
                    System.out.println(s[i]+" is a File\n");
                }
                else
                    System.out.println(s[i]+" is a Directory\n");
            }

            System.out.println("Total Number Of Files :"+cnt);
            System.out.println("Do You Want To Delete Files With Extension 'txt' (1/0) : ?");
            ch=Integer.parseInt(br.readLine());
            if(ch==1)
            {
                for(i=0;i<s.length;i++)
                {
                    File f=new File(dirname,s[i]);
                    if(f.isFile() && s[i].endsWith(ext))
                    {
                        System.out.println(s[i]+" -> deleted");
                        f.delete();
                    }
                }
            }
        }
    }
}
******************************************************************************
[root@localhost ~]# java DirDemo raj
raj Is a Directory

Contents Of : raj
con.php is a File

Total Number Of Files :1
Do You Want To Delete Files With Extension 'txt' (1/0) : ?
1
[root@localhost ~]#

************************************************************************************
****************************Ass:5.Set:A.Que:2*********************************
Que)      Write a java program to accept two file names as command line arguments and copy
the contains of first to second in such a manner the case of all alphabet is changed and
digits are replaced by ‘*’. Display appropriate error message if the first file does not exist.
(Use methods from Character class )



*********************************Java Code***********************************

import java.io.*;

   class file2
  {
   public static void main(String a[ ])throws IOException
   {
 
     FileInputStream f1=new FileInputStream(a[0]);
     FileOutputStream f2=new FileOutputStream("my.txt");
 
        int i;
      do
      {
        i=f1.read();
        if(i!=-1)
       {
        if(Character.isUpperCase((char)i))
          f2.write(Character.toLowerCase((char)i));
        else
        if(Character.isLowerCase((char)i))
          f2.write(Character.toUpperCase((char)i));
        else
        if(Character.isDigit((char)i))
          f2.write('*');
       }       
      }while(i!=-1);
      f1.close();
      f2.close();
   }
  }

OUTPUT:-

[root@localhost FileHandling]# vi seta2.java
[root@localhost FileHandling]# javac seta2.java
[root@localhost FileHandling]# java file2 pqr.txt
[root@localhost FileHandling]# vi my.txt
  
worldisbuitiful
date*******
dAYTUESDAY



******************************************************************************
****************************Ass:5.Set:A.Que:3*********************************
Que)Write a program to display the contents of a file in the reverse order.


*********************************Java Code***********************************

import java.io.*;
class seta3
{
 public static void main(String args[])throws IOException
  {
   FileInputStream fis=new FileInputStream("2.txt");
   BufferedInputStream bis=new BufferedInputStream(fis);
   System.out.println("The contents in reversed order are:");
   int size=fis.available();
   for(int i=size-1;i>=0;i--)
    {
      bis.mark(i);
      bis.skip(i);
      System.out.println((char)bis.read());
      bis.reset();
    }
   bis.close();
 }
}

******************************Output*****************************************
[root@localhost ass5]#java seta3
The contents in reverse order are:
ayaniv
[root@localhost ass5]#
*******************************************************************************

****************************Ass:5.Set:B.Que:2*********************************
Que)Write a program to store student information (roll number, name, percentage) in a
 RandomAccessFile “student.dat”. Display the details of the student having a
 specific roll number.



*********************************Java Code***********************************

import java.io.*;
class setb2
{
 public static void main(String args[])throws IOException
  {
   RandomAccessFile f=new RandomAccessFile("student.dat","rw");
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   int n,rno,perc,i;
   String name;
   String ch=" ";
   System.out,println("Enter how many records you want to?");
   n=Integer.parseInt(br.readLine());
   for(i=0;i<n;i++)
   {
     System.out.println("Enter the details of the "+(i+1)+"student");
     rno=Integer.parseInt(br.readLine());
     name=br.readLine();
     perc=Integer.parseInt(br.readLine());
     f.writeUTF(name);
     f.writeInt(perc);
    }
    //fseek(0);
   while(!ch.equals("n"))
    {
      f.seek(0);
      System.out.println("Enter the student roll no to be searched:");
      int no1;
      no1=Integer.parseInt(br.readLine());
      for(i=0;i<n;i++)
       {
         rno=f.readInt();
         name=f.readUTF();
         perc=f.readInt();
         if(rno==no1)
            System.out.println("Rno-"+rno+"\nName-"+name+"\nPerc-"+perc);
       }
      System.out.println("Do you want to continue? Y/N");
      ch=br.readLine();
    }
    f.close();
  }
}

********************************Output***************************************
[root@localhost~]#java setb2
Enter how many records you want to?
2
Enter the details of the 1 student
1
vinaya
65
Enter the details of the 2 student
2
rachna
62
Enter the student roll no to be searched:
1
Rno-1
Name-vinaya
Perc-65
Do you want to continue? Y/N
n
***********************************************************************************

****************************Ass:5.Set:B.Que:3*********************************
Que)      Write a Java program to accept an option, string and file name from user. Perform
following operations:
           a. If no option is passed then print all lines in the file containing the string.
           b. If the option passed is –c then print the count of lines containing the string.
           c. If the option passed is –v then print the lines not containing the string.
*********************************Java Code***********************************
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;

class Testfile
{
    public static void main(String args[])throws Exception
    {
        System.out.println("\t**Option**\n");
        System.out.println("1.No Option");
        System.out.println("2.Option'-c'");
        System.out.println("3.Option'-v'");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\n Enter input File name");
        String filename = br.readLine();
        System.out.println("Enter the option:");
        int option = Integer.parseInt(br.readLine());
        BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String str = "";
        String line = "";
        switch(option)
        {
            case 1:
                {
                    while(true)
                    {
                        line = br1.readLine();
                        if(line==null)
                            break;
                        else
                        {
                            System.out.println(line);
                        }
                    }



                }
                break;
            case 2:
                {
                    System.out.println("Enter the Input String");
                    str = br.readLine();
                    while(true)
                    {
                        line = br1.readLine();
                        if(line==null)
                                break;
                                else
                                {
                                if(line.indexOf(str)>-1)
                                {
                                System.out.println(line);   
                                }   
                                }
                                }




                                }
                                break;
                                case 3:
                                {
                                System.out.println("Enter the Input String");
                                str = br.readLine();
                                while(true)
                                {
                                    line = br1.readLine();
                                    if(line==null)
                                        break;
                                    else
                                    {
                                        if(line.indexOf(str)==-1)
                                        {
                                            System.out.println(line);
                                        }
                                    }
                                }
                                }
                                break;
                                default:
                                {
                                    System.out.println("Wroung Choice");
                                }

        }
    }
}
/*OUTPUT:

[root@localhost ~]# vim ass5b3.txt
Question:What is Abstract class
Ans.:
1.class which cannot be instaniated.
2.It is only used to ceate subclass.
3.it has abstract method must be Declared abstract.
4.It has data members & Constructor.
                                       
[root@localhost ~]# vim ass5b3.java +33
[root@localhost ~]# javac ass5b3.java
[root@localhost ~]# java Testfile
        **Option**

1.No Option
2.Option'-c'
3.Option'-v'

 Enter input File name
ass5b3.txt
Enter the option:
1
Question:What is Abstract class
Ans.:
1.class which cannot be instaniated.
2.It is only used to ceate subclass.
3.it has abstract method must be Declared abstract.
4.It has data members & Constructor.
[root@localhost ~]# java Testfile
        **Option**

1.No Option
2.Option'-c'
3.Option'-v'

 Enter input File name
ass5b3.txt
Enter the option:
2
Enter the Input String
class
Question:What is Abstract class
1.class which cannot be instaniated.
2.It is only used to ceate subclass.
[root@localhost ~]# java Testfile
        **Option**

1.No Option
2.Option'-c'
3.Option'-v'

 Enter input File name
ass5b3.txt
Enter the option:
3
Enter the Input String
class
Ans.:
3.it has abstract method must be Declared abstract.
4.It has data members & Constructor.
*/


******************************************************************************




No comments:

Post a Comment