用java创建行编辑器程序?

用java创建行编辑器程序?,java,editor,linked-list,line,Java,Editor,Linked List,Line,这是最初的问题: 您的程序将是一个行编辑器。行编辑器是通过在命令行中输入命令来执行所有操作的编辑器。命令包括显示行、插入文本、编辑行、剪切和粘贴文本、加载和保存文件。例如,用户输入三行文本并将其保存为新文件的会话可能显示为: Menu: m Delete line: dl Load file: l Delete range: dr Show all: sa Copy range: cr Show line: sl Pa

这是最初的问题: 您的程序将是一个行编辑器。行编辑器是通过在命令行中输入命令来执行所有操作的编辑器。命令包括显示行、插入文本、编辑行、剪切和粘贴文本、加载和保存文件。例如,用户输入三行文本并将其保存为新文件的会话可能显示为:

   Menu: m           Delete line:  dl
   Load file: l      Delete range:  dr
   Show all: sa      Copy range:  cr
   Show line:  sl    Paste lines:  pl
   Show range:  sr   Write to file:  w
   New line:  nl     Quit:  q
   Edit line:  el    Write and quit:  wq
->  nl

type line? (y/n):  y
1: this is the first line
type line? (y/n):  y
2: and a second line

type line? (y/n):  y
3: and this is the third line
type line? (y/n):  n

->  w

write to file:  test.txt

->  sa

1: this is the first line
2: and a second line
3: and this is the third line

->  q
创建的文件是:

this is the first line
and a second line
and this is the third line
编辑器将首先显示一个菜单,之后将提示输入带“->”的命令。可以在示例中出现的菜单中看到可以输入的命令

此网页是使用此处描述的行编辑器编写的。 命令

如果在没有命令行参数的情况下运行程序,它将以空文档开始。如果将现有文件的名称作为命令行参数指定,它将打开并加载该文件的内容。您可以使用quit、q或write and quit、wq命令退出程序。 显示菜单

该菜单在程序启动时显示。它可以通过命令m显示。 读写文件

load file命令l将要求用户输入文件名,打开文件进行读取,将文件内容读入编辑器并关闭文件。如果编辑器中已经有文本,则会将其丢弃。write file命令w将编辑器的内容写入文件。如果以前使用read file命令打开过某个文件,则该文件将用于写入。否则,程序将提示用户输入文件名。对于读取或写入文件命令,如果无法打开请求的文件,将显示相应的错误消息。 显示文本

文本可以通过三个命令显示:全部显示(sa)、显示范围(sr)和显示线(sl)。所有三个命令都显示带有行号的文本。全部显示显示整个文档。Show range将向用户询问一系列行(“从”和“到”),并显示该范围内的行。如果“收件人”行号大于文档行中的行数,则显示截至文档末尾的行数。Show line将要求用户输入行号并显示该行。 输入新的文本行

使用新行命令nl输入新的文本行。在输入新行之前,程序将询问键入行?(是/否)。如果用户选择“是”,则在显示行号后将接受该行。如果文档为空,则新行将是文档的第一行(第1行)。如果文档不是空的,程序将提示用户输入一行,在该行之后将放置新行。如果行号超出范围,程序将不接受新行。可以通过请求在(不存在的)第0行之后添加一行来添加新的第一行。用户输入一行后,程序将继续要求输入新行,直到用户回答“否”。在输入新行时,它们会按顺序添加到文档中。当用户开始在“新行”命令中输入行时,将首先显示添加新行之后的行(除非用户在文档开头输入行)。示例:

->  sa

1: this is the first line
2: and a second line
3: and this is the third line

->  nl
insert after line number:  1
inserting after:
this is the first line
type line? (y/n):  y
2: one and a half
type line? (y/n):  y

3: one and three quarters
type line? (y/n):  n

->  sa

1: this is the first line
2: one and a half
3: one and three quarters
4: and a second line
5: and this is the third line

->
缓冲器

有两个缓冲区,行缓冲区和字符串缓冲区,用于移动文本块。行缓冲区保存整行的范围,字符串缓冲区保存各行的子字符串。每当一个新的行范围被复制到行缓冲区或一个字符串被复制到字符串缓冲区时,该缓冲区先前的内容将被删除(但另一个缓冲区不受影响)。 线的移动范围

可以使用复制范围cr和粘贴行pl移动行块。复制范围将要求用户提供行号范围(“从”和“到”),并将行复制到行缓冲区-行在文档中保持不变。“粘贴行”将要求用户输入行的编号,之后将粘贴行缓冲区中的行。然后将行复制到文档中-行缓冲区保持不变。使用delete range(删除范围)删除行的范围。再次要求用户提供要删除的行号范围。然后删除行(行缓冲区不变)。 编辑一行文本

“编辑行”命令el将要求输入要编辑的行的编号,然后显示前面带有刻度的行,该刻度允许用户按编号(从0开始)识别行中的位置。然后显示line菜单,程序提示输入

->  el

line number:  3
0    5   10   15   20 
|----+----|----+----|-
one and three quarters

   Show line:  s
   Copy to string buffer:  c
   Cut:  t
   Paste from string buffer:  p
   Enter new substring:  e
   Delete substring:  d
   Quit line:  q
->  
比例至少与显示的线一样长。然后,从“行”菜单中选择的操作将在此选择的行上操作,直到退出行q

Show line(显示线)将(再次)显示带有刻度的线。退出返回到主编辑器菜单。其余命令编辑选定的行

“复制到字符串缓冲区”和“从字符串缓冲区粘贴”的工作方式与“复制范围”和“粘贴行”类似,但它们使用的是字符串缓冲区而不是行缓冲区。此外,“从字符串缓冲区粘贴”将插入子字符串,使其从指定位置开始。(因此,它执行“在之前插入”,而粘贴行执行“在之后插入”。)

Delete substring,t将提示要删除的字符范围,将显示带下划线的字符串,询问用户范围是否正确,然后删除该子字符串

->  el

line number:  1
0    5   10   15   20 
|----+----|----+----|-
this is the first line

   Show line:  s
   Copy to string buffer:  c
   Cut:  t
   Paste from string buffer:  p
   Enter new substring:  e
   Delete substring:  d
   Quit line:  q
->  d

0    5   10   15   20 
|----+----|----+----|-
this is the first line
from position:  12
to position:  18
delete:
0    5   10   15   20 
|----+----|----+----|-

this is the first line
            ^^^^^^^
y/n:  y
0    5   10   15
|----+----|----+
this is the ine
剪切、t组合复制到字符串缓冲区和删除子字符串。它将子字符串复制到字符串缓冲区,并将其从行中删除

输入新的子字符串e,将允许用户在现有行中键入新文本

line number:  1
0    5   10   15   20
|----+----|----+----|-
this is the first line

   Show line:  s
   Copy to string buffer:  c
   Cut:  t
   Paste from string buffer:  p
   Enter new substring:  e
   Delete substring:  d
   Quit line:  q
->  e

0    5   10   15   20
|----+----|----+----|-
this is the first line
insert at position:  12
text:  very very
0    5   10   15   20   25   30
|----+----|----+----|----+----|-
this is the very very first line
内部构件

您将定义(除其他外)两个类:行类和文档类。Line类将文档中的一行文本保存在String类型的字段中,并提供对一行的基本操作。其中一些操作是在指定位置de向行中插入字符串
line number:  1
0    5   10   15   20
|----+----|----+----|-
this is the first line

   Show line:  s
   Copy to string buffer:  c
   Cut:  t
   Paste from string buffer:  p
   Enter new substring:  e
   Delete substring:  d
   Quit line:  q
->  e

0    5   10   15   20
|----+----|----+----|-
this is the first line
insert at position:  12
text:  very very
0    5   10   15   20   25   30
|----+----|----+----|----+----|-
this is the very very first line
String substring(int beginIndex)  // Returns a new string that is a
                                  // substring of this string.
String substring(int beginIndex, int endIndex)  // Returns a new string
                                                // that is a substring of
                                                // this string.