File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Selective retrieval of numbers in a text file

 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text file as follows

I have to retrieve only those values which fall in the mem, swap and total under total, used and free i.e. I have to ignore 0, 564, 3237, 4074, 3848. How do I do it?

I have written the following code to display the all the numbers.
 
Swastik Dey
Rancher
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the field separator between the columns?
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You might want to read the text files line by line. Now you read it token by token using the next() method.
2. You have to check for matches in the lines read such as whether its for memory / swap / total.
3. Read only the first three integer tokens from the line being processed - but make sure always total used and free sizes are always present. Else you might end up reading shared, buffers, and even cached values.

Below piece of code might help you to build a scanner using the lines being read.



And make the lineScanner act as the myFile scanner you currently have. The lineScanner should act on all the tokens present in a single line.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an extra piece of information:
Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:What is the field separator between the columns?

A couple of space.

John Jai wrote:
1. You might want to read the text files line by line. Now you read it token by token using the next() method.
2. You have to check for matches in the lines read such as whether its for memory / swap / total.
3. Read only the first three integer tokens from the line being processed - but make sure always total used and free sizes are always present. Else you might end up reading shared, buffers, and even cached values.

Thanks!! I will work on this and get back.

Mohamed Sanaulla wrote:Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.

I do have control over the file format. How is json going to help? I am very new to java.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Just an extra piece of information:
Do you have a control over the format of the file? Or is it always a text file.
If you have control over the file contents or the format- you might want to consider using xml/json format.


As an extension to what I am doing currently, I have to be able to plot this into a graph (a pie chart or a bar graph). In that case, would json be more helpful than a textfile?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue with the text file is that you may not be sure of the structure- there may be 4 spaces, or a tab or a new line seperating the data.

But with a structured document like XML or JSON you are for sure about the structure of the document and you can use parser api's already written to parse your XML or JSON documents.
 
rastogi payam
Ranch Hand
Posts: 47
Chrome Eclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help you...
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic