In: Computer Science
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
package fileio; import java.io.*; import java.util.Scanner; public class Fileio { public static void append(String fn,String str) throws IOException{ //function to append the details of the student BufferedWriter out = new BufferedWriter(new FileWriter(fn, true)); out.write(str+'\n'); out.close(); } public static void main(String[] args) throws IOException { Scanner s=new Scanner(System.in); String sname,sadd,sdob; System.out.println("Enter Student Name: "); sname=s.nextLine(); System.out.println("Enter Student Address: "); sadd=s.nextLine(); System.out.println("Enter Student DOB: "); sdob=s.nextLine(); System.out.println("Storing in the File...."); BufferedWriter bf=new BufferedWriter(new FileWriter("myfile.txt")); //making the use of Buffered Writer to write in a file bf.write(sname+'\n'); bf.close(); append("myfile.txt",sadd); append("myfile.txt",sdob); System.out.println("Printing the File..."); BufferedReader in = new BufferedReader(new FileReader("myfile.txt")); //Similarly for reading using Buffered Reader class String mystring; while ((mystring = in.readLine()) != null) { System.out.println(mystring); } } }
Here the above program demonstrates the task which was required to be done in the above question using Java. Also the screenshot of the output is being attached for any further reference. Hope it helps....