Skip to main content

File handling in java: Write a program in java to store 5 student name and marks in a text file. Then display all the values

 Write a program in java to store 5 student name and marks in a text file. Then display all the values.

import java.util.*;

import java.io.*;

public class Info

{

    public static void main(String args[])throws IOException

    {

        FileWriter fw = new FileWriter("studentinfo.txt",true);

        BufferedWriter bw = new BufferedWriter(fw);

        PrintWriter pw = new PrintWriter(bw);

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the number of informations:");

        int n = sc.nextInt();

        for(int i=0;i<n;i++)

        {

            System.out.println("Enter student name:");

            String name = sc.next();

            System.out.println("Enter marks:");

            double marks = sc.nextDouble();

            pw.println(name);

            pw.println(marks);

        }

        sc.close();

        pw.close();

        bw.close();

        fw.close();

        //reading the text file

        Scanner read = new Scanner(new File("studentinfo.txt"));

        System.out.println("\nThe student information are:");

        while(read.hasNext())

        {

            String name = read.next();

            double marks = read.nextDouble();

            System.out.println("Name:"+name+"\nMarks:"+marks);

        }

        read.close();

    }

}

Popular posts from this blog