//File: edit.java //CS 471 - Operating Systems Project //Dennis Pereira, Ely Soto, John Cavalieri //Edit is a command that is part of the baseline simulator //it is a simple program the takes a filename as an argument //or if no file name is given, then the user is prompted for one //The file is created and all text typed from that point forward //is saved in the file //the user can stop the edit command by typing a blank line //this will end the edit command, and close the file for editing import java.io.*; public class edit extends VMachine { /** Creates new edit */ public edit() { } public void run() { RandomAccessFile editstream = null; try { //string variable for the filename String input; if there is a file, then read it into the args array if(args.length > 0) { input = args[0]; } else { //no file was given in the command, so prompt the user for one sysapi.Write(OUT,"Please enter the file to edit: "); input = sysapi.Read(IN); } //see if the user has typed a blank line if(input.compareTo("")!=0) { //create the file in the users path File editFile = new File(sysapi.findUser(owner).getcurrentPath() + input); editFile.createNewFile(); editstream = new RandomAccessFile(editFile,"rw"); sysapi.Write(OUT,"Editing file: " + input + "\n"); //while the user hasn't typed a blank line, continue to read //the user input while(input.compareTo("")!=0) { input = sysapi.Read(IN); if(input.compareTo("")!=0) { editstream.writeBytes(input + "\n"); } } sysapi.Write(OUT,editFile.getName() + " saved.\n"); editstream.close(); } else { //the user did not type a name when prompted sysapi.Write(OUT,"No file edited.\n"); } } catch(Exception ex) { //something out of the ordinary happened System.out.println("edit threw Exception: " + ex); } sysapi.Exit(vmh,owner); } }