提交 789b03b1 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 db613091
...@@ -33,6 +33,7 @@ public class FileViewer extends Tool { ...@@ -33,6 +33,7 @@ public class FileViewer extends Tool {
out.println("java "+getClass().getName() + "\n" + out.println("java "+getClass().getName() + "\n" +
" -file <file> The name of the file to view\n" + " -file <file> The name of the file to view\n" +
" [-find <text>] Find a string and display the next lines\n" + " [-find <text>] Find a string and display the next lines\n" +
" [-start <x>] Start at the given position\n" +
" [-head] Display the first lines\n" + " [-head] Display the first lines\n" +
" [-tail] Display the last lines\n" + " [-tail] Display the last lines\n" +
" [-lines <x>] Display only x lines (default: 30)\n" + " [-lines <x>] Display only x lines (default: 30)\n" +
...@@ -46,12 +47,15 @@ public class FileViewer extends Tool { ...@@ -46,12 +47,15 @@ public class FileViewer extends Tool {
boolean head = false, tail = false; boolean head = false, tail = false;
int lines = 30; int lines = 30;
boolean quiet = false; boolean quiet = false;
long start = 0;
for (int i = 0; args != null && i < args.length; i++) { for (int i = 0; args != null && i < args.length; i++) {
String arg = args[i]; String arg = args[i];
if (arg.equals("-file")) { if (arg.equals("-file")) {
file = args[++i]; file = args[++i];
} else if (arg.equals("-find")) { } else if (arg.equals("-find")) {
find = args[++i]; find = args[++i];
} else if (arg.equals("-start")) {
start = Long.decode(args[++i]).longValue();
} else if (arg.equals("-head")) { } else if (arg.equals("-head")) {
head = true; head = true;
} else if (arg.equals("-tail")) { } else if (arg.equals("-tail")) {
...@@ -77,20 +81,21 @@ public class FileViewer extends Tool { ...@@ -77,20 +81,21 @@ public class FileViewer extends Tool {
head = true; head = true;
} }
try { try {
process(file, find, head, tail, lines, quiet); process(file, find, head, tail, start, lines, quiet);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void process(String fileName, String find, boolean head, boolean tail, int lines, boolean quiet) throws IOException { private void process(String fileName, String find, boolean head, boolean tail, long start, int lines, boolean quiet) throws IOException {
RandomAccessFile file = new RandomAccessFile(fileName, "r"); RandomAccessFile file = new RandomAccessFile(fileName, "r");
long length = file.length(); long length = file.length();
if (head) { if (head) {
file.seek(0); file.seek(start);
list(0, "Head", readLines(file, lines)); list(start, "Head", readLines(file, lines));
} }
if (find != null) { if (find != null) {
file.seek(start);
long pos = find(file, find.getBytes(), quiet); long pos = find(file, find.getBytes(), quiet);
if (pos >= 0) { if (pos >= 0) {
file.seek(pos); file.seek(pos);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论