aspose file tools
The moose likes Java in General and the fly likes This is the Code to search a string in the last 100 lines of a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "This is the Code to search a string in the last 100 lines of a file. " Watch "This is the Code to search a string in the last 100 lines of a file. " New topic
Author

This is the Code to search a string in the last 100 lines of a file.

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 164
This code is throwing an error when the pattern is '{' but not in any other case
Antany Vasanth
Ranch Hand

Joined: Jan 28, 2009
Posts: 43
Hi Jacob,

The "{" is used in pattern to specify the number for occurances. For example \d{2} means two digits.

So if you want to use the pattern "{". You need to escape it.

So try "\\{" instead of "{"

Regards,
Antany.
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 164
Thanks for it. Also please help me in making the program efficient. Here i am reading the file twice to find the number of lines and then go to the last 100 lines, there must be an efficient way. Please help me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

How about using a List to store the 100 last records?
I've once written (and thrown away...) a List implementation that used a fixed size array and cycled through that; if the List would grow too large the first element was automatically removed. In essence, it used the following structure:
- Object[] elements - the elements
- int start - the index inside elements of the first element
- int end - the index inside elements one past the last element
- int size - the number of elements

Essentially, in a full list, start == end. By adding one element, both start and end get increased by one (reset to 0 if >= elements.length), thereby removing the first element. size is usually (end + elements.length - start) % elements.length but is needed when start == end: that can either mean an empty list or a full list.

The reason I've thrown this class away is that it was quite a chore to insert elements somewhere in the middle, or remove elements. It shifted either up or down, whichever would have the least number of elements shifted.
Perhaps I should recreate the class but this time as a Collection that does not allow removal, only addition.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 164
hey thanks
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: This is the Code to search a string in the last 100 lines of a file.
 
Similar Threads
Writing to File
FileIO
search files in directory for string and return count
# of lines in a file?
Create .txt file