Written by Sai Sandeep Thota on November 5, 2013 in Java > Programming

Java Program to create Student Database and Save to File

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Maintaining a soft copy of files is always a good idea. Coming to the Colleges / Universities a subject Professor would like to get the things simplified by having a handy soft copy instead of writing all the information about a student like Name, Roll Number, Marks etc.,
In this program we are going to take Number of Students, Student Name, Roll Number and Marks as input. We have 2 classes and 3 methods in total. studentDetails class have getDetails and display methods which are used to get the details of the Student and Display details of the Student respectively.

In the collegeOffice class we have main method in which we have created an object of PrintStream class. Using that object we are creating a text file called StudentDetails.txt and appending Student Details into it.

Java Program to create Student Database and Save to File

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

class studentDetails {
	public String name[][] = new String[10][10];
	void getDetails(int n)
	 {
		Scanner get = new Scanner(System.in);
		int limit=n;
		System.out.println("Enter "+limit+" Student Details\n");

		for(int i=0;i<limit;i++)
		{
			System.out.println("Enter Student #"+(i+1)+" Name, Roll Number & Marks:");
			for(int j=0;j<3;j++)
			{
				name[i][j] = get.nextLine();
			}
		}
		
		display(limit);
	}

	void display(int limit)
	{
		System.out.println("Student Name"+"\t"+"Roll"+"\t\t"+"Marks");
		for(int i=0;i<limit;i++)
		{
			for(int j=0;j<3;j++)
			{
				System.out.print(name[i][j]+"\t\t");
			}
		System.out.println();
		}
	}
	
	
}

class collegeOffice {
	public static void main(String args[]) throws IOException
	{	
		Scanner in  =  new Scanner(System.in);
		System.out.print("Enter Number of Students:");
		int n = in.nextInt();
		studentDetails std = new studentDetails();
		std.getDetails(n);

			
			PrintStream output = new PrintStream(new File("StudentDetails.txt"));
			output.println("Student Name"+"\t"+"Roll"+"\t\t"+"Marks");
			output.println("======================================");
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<3;j++)
				{
					output.print(std.name[i][j]+"\t\t");
				}
			output.println();
			output.println("======================================");
			}
			output.close();

	}
}

Output:
college_office_marks

Download Source Code

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: