Tuesday, 23 January 2018

Create a class called Student. Write a student manager program to manipulate the student information from files by using FileInputStream and FileOutputStream.


Program:-
import java.io.*;
import java.util.*;


class studentmanager
{
            String name;
            int rollno;
           
            static Scanner s = new Scanner(System.in);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  
    void getinfo()
    {
                        System.out.println("Enter Student Roll No: ");     
                        rollno = s.nextInt();
                        System.out.println("Enter Student Name: ");
                        try{     
                        name = br.readLine();
                        }catch(Exception e){System.out.println("Exception occurred...");}
            }
           
    void addinfo()
            {
                        try
                        {
                                    FileOutputStream os = new FileOutputStream("studentinfo.txt",true);
                                   
                                    byte b[]=name.getBytes();
                                    os.write((char)rollno);
                                    os.write('\n');
                                    os.write(b); 
                                    os.write('\n'); 
            os.close();
                                   
                                    System.out.println("Success...");     
                        }
                        catch(Exception e)
                        {
                                    System.out.println("IO Exception Occured...");
                        } 
            }
           
            void readinfo()
            {
                          try
                          {
                                    FileInputStream fin=new FileInputStream("studentinfo.txt");   
                           
                                    while(true)
            {
                int a=fin.read();
                if(a==-1)
                    break;
                System.out.println("Roll No: "+a);
                a=fin.read();
                System.out.println("Name: ");
                do
                {
                    a=fin.read();
                    System.out.print((char)a);
                }while(a!='\n');
            }
                          }
          catch(Exception e)
          {
                                      System.out.println(e);
                          }   
            }
}

class u81 extends studentmanager
{
            public static void main(String a[])
            {
                        int ch;
                        char c='y';
                        while(c=='y')
                        {
                                    System.out.println("Enter Choice: \n1.Add Student Detail \n2.Show Data");
                                    ch=s.nextInt();
                                    studentmanager o = new studentmanager();
                                   
                                    switch(ch)
                                    {
                                                case 1:
                                                                        o.getinfo();
                                                                        o.addinfo();
                                                                        break;
                                                case 2:
                                                                        o.readinfo();
                                                                        break;
                                                default:
                                                                        System.out.println("Incorrect Menu Option..");
                                    }
                                    System.out.println("Continue? Press y :Exit-Press n");
                                    c=s.next().charAt(0);
                        }                     
            }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...