Please write a code in JAVA to do line editor..
Your program will be a line editor. A line editor is an editor where all operations are performed
by entering commands at the command line. Commands include displaying lines, inserting text,
editing lines, cutting and pasting text, loading and saving files. For example, a session where the
user enters three lines of text and saves them as a new file may appear as:
The file created is:
The editor will first display a menu after which it will prompt for commands with \"-> \". The
commands which can be entered can be seen in the menu appearing in the example.
This web page was written with the line editor described here.
The Commands
If you run the program with no command line arguments it will begin with an empty document.
If you give the name of an existing file as a command line argument it will open and load the
contents of that file. You can exit the program with either the quit, q, or write and quit, wq,
command.
Displaying the menu
The menu is displayed when the program starts. It can be displayed with the command m.
Reading and writing files
The load file command l will ask the user for a file name, open the file for reading, read the file
contents into the editor and close the file. If there is already text in the editor it is discarded. The
write file command w will write the contents of the editor to a file. If a file has previously been
opened with the read file command that same file is used for writing. Otherwise, the program
will prompt the user for a file name. For either the read or write file commands if the requested
file cannot be opened an appropriate error message is displayed.
Displaying text
Text can be displayed with three commands: show all (sa), show range (sr), and show line (sl).
All three commands display text with line numbers. Show all displays the entire document. Show
range will ask the user a range of lines (\"from\" and \"to\") and will display the lines in that
range. If the \"to\" line number is greater than the number of lines in the document lines up to the
end of the document will be displayed. Show line will ask the user for a line number and display
that line.
Entering new lines of text
New lines of text are entered using the new line command nl. Before entering a new line the
program will ask type line? (y/n). If the user chooses yes the line will be accepted after the line
number is displayed. If the document is empty the new line will be the first line (line 1) of the
document. If the document is notempty the program will prompt the user to enter the line after
which the new line will be placed. If the line number is out of range the program will not accept
a new line. A new first line can be added by asking to add a line after (the non-existent) line 0.
After the user has entered a line the program will continue asking for new lines until the user
answers no. As the new lines are being entered they are added sequentially to the document. As
the user be.
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Please write a code in JAVA to do line editor..Your program will b.pdf
1. Please write a code in JAVA to do line editor..
Your program will be a line editor. A line editor is an editor where all operations are performed
by entering commands at the command line. Commands include displaying lines, inserting text,
editing lines, cutting and pasting text, loading and saving files. For example, a session where the
user enters three lines of text and saves them as a new file may appear as:
The file created is:
The editor will first display a menu after which it will prompt for commands with "-> ". The
commands which can be entered can be seen in the menu appearing in the example.
This web page was written with the line editor described here.
The Commands
If you run the program with no command line arguments it will begin with an empty document.
If you give the name of an existing file as a command line argument it will open and load the
contents of that file. You can exit the program with either the quit, q, or write and quit, wq,
command.
Displaying the menu
The menu is displayed when the program starts. It can be displayed with the command m.
Reading and writing files
The load file command l will ask the user for a file name, open the file for reading, read the file
contents into the editor and close the file. If there is already text in the editor it is discarded. The
write file command w will write the contents of the editor to a file. If a file has previously been
opened with the read file command that same file is used for writing. Otherwise, the program
will prompt the user for a file name. For either the read or write file commands if the requested
file cannot be opened an appropriate error message is displayed.
Displaying text
Text can be displayed with three commands: show all (sa), show range (sr), and show line (sl).
All three commands display text with line numbers. Show all displays the entire document. Show
range will ask the user a range of lines ("from" and "to") and will display the lines in that
range. If the "to" line number is greater than the number of lines in the document lines up to the
end of the document will be displayed. Show line will ask the user for a line number and display
that line.
Entering new lines of text
New lines of text are entered using the new line command nl. Before entering a new line the
program will ask type line? (y/n). If the user chooses yes the line will be accepted after the line
number is displayed. If the document is empty the new line will be the first line (line 1) of the
document. If the document is notempty the program will prompt the user to enter the line after
2. which the new line will be placed. If the line number is out of range the program will not accept
a new line. A new first line can be added by asking to add a line after (the non-existent) line 0.
After the user has entered a line the program will continue asking for new lines until the user
answers no. As the new lines are being entered they are added sequentially to the document. As
the user begins to enter lines at the new line command the line after which the new line is being
added is first displayed (unless the user is entering lines at the beginning of the document).
Examples:
The buffers
There are two buffers, the line buffer and the string buffer, used for moving blocks of text. The
line buffer holds ranges of entire lines and the string buffer holds substrings of individual lines.
Whenever a new range of lines is copied to the line buffer or a string is copied to the string
buffer the previous contents of that buffer are deleted (but the other buffer is unaffected).
Moving ranges of lines
Blocks of lines can be moved using copy range, cr, and paste lines, pl. Copy range will ask the
user for a range of line numbers ("from" and "to") and will copy the lines to the line buffer -
the lines remain unchanged in the document. Paste lines will ask the user to enter the number of
the line after which the lines from the line buffer will be pasted. The lines are then copied into
the document - the line buffer is left unchanged. Ranges of lines are deleted with delete range, dr.
Again, the user is asked for the range of line numbers to delete. The lines are then deleted (the
line buffer is unchanged).
Editing a line of text
The edit line command, el will ask for the number of a line to edit then display the line preceded
by ascalewhich allows the user to identify positions in the line by number (beginning with 0).
Then the line menu is displayed and the program prompts for input.
The scale is at least as long as the line displayed. Operations chosen from the line menu will then
operate on this chosen line until quit line, q.
Show line will display the line (again) with the scale. Quit returns to the main editor menu. The
remaining commands edit the chosen line.
Copy to string buffer and Paste from string buffer work like copy range and paste lines but they
work with the string buffer rather than the line buffer. Also, Paste from string buffer will insert
the substring so that it begins at the specified position. (Thus it does an "insert before" where
paste lines does an "insert after.")
Delete substring, t, will prompt for a range of characters to delete, will display the string with the
substring underlined, ask the user if the range is ok, and then delete the substring.
Cut, t, combines copy to string buffer and delete substring. It copies the substring to the string
buffer and removes it from the line.
3. Enter new substring, e, will allow the user to type new text into an existing line.
Internals
You will define (among others) two classes: a Line class and a Document class. The Line class
will hold the text of a line from the document in a field of type String and provide basic
operations on a line. Some of these operations are insert a string into the line at a specified
position, delete a substring, copy a substring, etc. The Line class will also have a field with the
number of characters stored in the line. The Document class will hold an entire document (each
line in one String) and provide basic operations such as inserting lines, deleting, copying, and
pasting ranges of lines. It will also have a field with the number of lines currently in the
document. Elsewhere in your program you will provide code for the operations (chosen by the
user from the menu) which will then call the member functions of the Line and Document
classes.
The document will be a doubly linked list of lines (Strings) with a dummy node. I.e., if there are
100 lines in the document there will be 101 nodes in the linked list - 100 holding text and one
dummy node. This will make insertions and deletions much easier by eliminating special
boundary conditions. Line numbers will not be stored with lines. Instead, every time a line
number is needed (e.g. for display in show range and show all, finding positions for new line,
etc.) it will be found by traversing the list and counting.
All strings will be Java Strings. Lines of text can be manipulated with concatenation and the
substring methods:
To accept a new line of user input you must use nextLine since reading with next stops accepting
input at the first white space and text lines may contain white space. You must usenextLine for
all keyboard input: nextLine does not mix well with next, nextInt, etc., so to read integer input
you will first read a String and then convert it to an integer using Integer.parseInt.
You will do appropriate input validation. When choosing operations (in response to the "->"
prompt) your program will only accept input as stated in the menu. When accepting integer input
(line numbers, character positions in a line) your program will check for valid numbers and
ranges.
Solution
import java.io.File; // this import a package called file to perform file operations public class
MyFileOperations { public static void main(String[] a){ try{
File file = new File("fileName"); //opens the file
System.out.println(file.canRead()); //Tests whether the application can modify the
file System.out.println(file.canWrite()); //Tests whether the
application can modify the file System.out.println(file.createNewFile());
4. //Deletes the file or directory System.out.println(file.delete());
//Tests whether the file or directory exists. System.out.println(file.exists());
//Returns the absolute pathname string.
System.out.println(file.getAbsolutePath()); //Tests whether the file is a directory
or not. System.out.println(file.isDirectory()); //Tests whether the
file is a hidden file or not. System.out.println(file.isHidden());
//Returns an array of strings naming the //files and directories in the directory.
System.out.println(file.list()); } catch(Exception ex){
} } }