943,907 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2422
  • Java RSS
Jun 8th, 2009
1

populate array list from text file

Expand Post »
Good morning,

I am quite new at java and in the learning process.

I have a log file with the following data contained in it:
mai 4 03:26:53 pcjournal Dock[345]: Corrupt JPEG data: premature end of data segment
mai 4 03:26:53 pcbookscuba [0x1-0xa01a].com.apple.dock[345]: Tue Jun 2 03:26:53 macbookscuba.local Dock[215] <Error>: Corrupt JPEG data: premature end of data segment

I would like to read my text file and parse it and print out statistics for number of messages per minute, number of messages per hour, number of messages per day.

I am thinking of using arraylist to hold the data, perhaps a list of arraylist to hold each line I'm not sure.

So far I can read the text file holding the data:
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. class FileReadTest {
  4.  
  5. public static void main (String[] args) {
  6. FileReadTest f = new FileReadTest();
  7. f.readMyFile();
  8. }
  9.  
  10. @SuppressWarnings("deprecation")
  11. void readMyFile() {
  12.  
  13. DataInputStream dis = null;
  14. String record = null;
  15. int recCount = 0;
  16.  
  17. try {
  18.  
  19. File f = new File("mydata.txt");
  20. FileInputStream fis = new FileInputStream(f);
  21. BufferedInputStream bis = new BufferedInputStream(fis);
  22. dis = new DataInputStream(bis);
  23.  
  24. while ( (record=dis.readLine()) != null ) {
  25. recCount++;
  26. System.out.println(recCount + ": " + record);
  27. }
  28.  
  29. } catch (IOException e) {
  30. // catch io errors from FileInputStream or readLine()
  31. System.out.println("IOException error!" + e.getMessage());
  32.  
  33. } finally {
  34. // if the file opened okay, make sure we close it
  35. if (dis != null) {
  36. try {
  37. dis.close();
  38. } catch (IOException ioe) {
  39. }
  40. }
  41. }
  42. }
  43. }

The problem I am having is that I do not know how to go about obtaining the result I am looking for.
Could somebody guide me in the right direction? Perhaps a tutorial or an example I could work with.

THank you very much for your help,
John
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
JohnPhilipps is offline Offline
65 posts
since Jun 2009
Jun 8th, 2009
0

Re: populate array list from text file

It depends on how you want to parse it.
If you managed to read each line in the 'record' variable perhaps you should look at the String class. There are methods like split() and substring()
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jun 8th, 2009
0

Re: populate array list from text file

Thanks I will look into these two methods.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
JohnPhilipps is offline Offline
65 posts
since Jun 2009
Jun 8th, 2009
0

Re: populate array list from text file

You may want to look into Regular Expressions as well.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 8th, 2009
0

Re: populate array list from text file

Thank you
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
JohnPhilipps is offline Offline
65 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Calculator (value of an arithmetic expression)
Next Thread in Java Forum Timeline: String input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC