Hey I need help writing a java program that creates a file and write on it then
ID: 3594770 • Letter: H
Question
Hey I need help writing a java program that creates a file and write on it then reports an output of the file by sorting it in alpabethitical order.
import java.util.*; public class readFilei private Scanner m; public static void main(String args)i AccessFíle b = new AccessFile(); b.openFileO; b.AccessFile(; b.closeFile; b . J , b . s , b . e) ; System . out . printf("%s %s %s ", b.AccessFile(; public void openFileO1 m-new Scanner(new FileC"votes.text"); catch(Exception e)i System.out.printlnC"fille not found");Explanation / Answer
Copy the code in FileSort.java and then compile and run.
Compile Comamnd : javac FileSort.java
Run Command : java FileSort
## Please note that the program will generate a file named with "input.txt" in the current folder and overwrite any file if exists with the same name. Please have a backup of such file in-case you have in this program folder.
Code Snippet :
import java.io.*;
import java.util.*;
public class FileSort{
public static void main(String [] args){
List<String> inputList = new ArrayList<String>();
String filePath = "input.txt";
File file = new File(filePath);
FileWriter writer = null;
BufferedReader bufferRead = null ;
String inputLine;
try{
writer = new FileWriter(file);
bufferRead = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter new Line and Enter/Just Enter to exit the entry : ");
inputLine = bufferRead.readLine();
while(inputLine != null && inputLine.length()!= 0) {
System.out.print("Enter new Line and Enter : ");
writer.write(inputLine);
inputList.add(inputLine);
writer.write(" ");
writer.flush();
inputLine = bufferRead.readLine();
}
Collections.sort(inputList);
System.out.println("The input in sorted order :");
for (String s : inputList){
System.out.println(s);
}
} catch (Exception e) {
System.out.println("Some Input/Output Exception happened :");
e.printStackTrace();
} finally {
if(writer != null) {
try {
writer.close();
} catch (IOException e) {
System.out.println("Some Input/Output Exception happened :");
e.printStackTrace();
}
}
if (bufferRead != null){
try {
bufferRead.close();
}
catch (IOException e) {
System.out.println("Some Input/Output Exception happened :");
e.printStackTrace();
}
}
}
}
}