Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
Simple tail utility
Bhupinder Dhillon
Ranch Hand
Posts: 124
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Any ideas on how to improve the performance for showFromEnd() method OR better yet is there any professional implementation of tail utility available?
import java.io.FileReader; import java.io.IOException; import java.io.FileNotFoundException; import java.io.LineNumberReader; public class tail { private String file; private int lines; public tail (String file){ this(file, 10); } public tail (String file, int lines){ this.file = file; this.lines = lines; } public void setLines(int lines){ this.lines = lines; } public int showFromStart(){ int status = 0; LineNumberReader lineReader = null; try { lineReader = new LineNumberReader(new FileReader(this.file)); String line = null; while ((line = lineReader.readLine()) != null && lineReader.getLineNumber() <= lines){ System.out.println(line); } } catch (FileNotFoundException fnfe){ System.err.println("File not found"); status = 2; } catch (IOException ie){ System.err.println(ie.getMessage()); status = 1; } finally { try { if (lineReader != null) lineReader.close(); } catch (IOException ignore){} return status; } } public int showFromEnd () { int status = 0; LineNumberReader lineReader = null; try { lineReader = new LineNumberReader(new FileReader(this.file)); //get to end of file while (lineReader.readLine() != null){} int totalLines = lineReader.getLineNumber(); //close reader lineReader.close(); int targetLineNumber = 0; if (totalLines > lines) targetLineNumber = (totalLines - lines); //re-open reader lineReader = new LineNumberReader(new FileReader(this.file)); String line = null; while ((line = lineReader.readLine()) != null){ if (lineReader.getLineNumber() >= targetLineNumber) System.out.println(line); } } catch (FileNotFoundException fnfe){ System.err.println("File not found"); status = 2; } catch (IOException ie){ System.err.println(ie.getMessage()); status = 1; } finally { try { if (lineReader != null) lineReader.close(); } catch (IOException ignore){} return status; } } public static void main (String args[]){ int number = 10; boolean fromBegining = false; if (args.length < 1){ System.out.println("Usage: java tail fileName [n] [true/false]"); System.exit(1); } else if (args.length > 1){ number = Integer.parseInt(args[1], 10); if (args.length > 2 && args[2].equalsIgnoreCase("true")) fromBegining = true; } //long startTime = System.currentTimeMillis(); tail t = new tail(args[0], number); int status = 0; if (fromBegining) status = t.showFromStart(); else status = t.showFromEnd(); //System.out.println("Elapsed Time: " + (System.currentTimeMillis() - startTime) + " milliseconds"); System.exit(status); } }
[ February 26, 2003: Message edited by: Bhupinder Dhillon ]
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
why wont this run
Where did the contents of my .txt. file go?
How to Remove Main()
UNIX 'tail' clone - implementation help
StringTokenizer Class......
More...