
Write a program that stores the names of four employees in an array of strings. Store the contents of the array in an alphabetical order in a file and display it back on console. Do not use the RandomAccessFile class*/

In the given code, we have declared an array of string and accepts the input from the user and stored into array. Then using the Arrays.sort() method, we have sorted the string array value and stored the string into the file. After that, we have displayed the file data on the console.
import java.io.*;
import java.util.*;
class FileHandling
{
public static void main(String[] args)
{
File f=new File("c:/emp.txt");
String employees[]=new String[4];
try{
Scanner input=new Scanner(System.in);
System.out.println("Enter names of employees: ");
for(int i=0;i<employees.length;i++){
employees[i]=input.nextLine();
}
Arrays.sort(employees);
BufferedWriter bw=new BufferedWriter(new FileWriter(f,true));
for(int i=0;i<employees.length;i++){
bw.write(employees[i]);
bw.newLine();
}
bw.close();
System.out.println("Employees Name in Ascending Order: ");
BufferedReader br=new BufferedReader(new FileReader(f));
String str="";
while((str=br.readLine())!=null){
System.out.println(str);
}
}
catch(Exception e){}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.