populate array list from text file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 27
Reputation: JohnPhilipps is an unknown quantity at this point 
Solved Threads: 0
JohnPhilipps JohnPhilipps is offline Offline
Light Poster

populate array list from text file

 
1
  #1
Jun 8th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: populate array list from text file

 
0
  #2
Jun 8th, 2009
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()
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 27
Reputation: JohnPhilipps is an unknown quantity at this point 
Solved Threads: 0
JohnPhilipps JohnPhilipps is offline Offline
Light Poster

Re: populate array list from text file

 
0
  #3
Jun 8th, 2009
Thanks I will look into these two methods.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: populate array list from text file

 
0
  #4
Jun 8th, 2009
You may want to look into Regular Expressions as well.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 27
Reputation: JohnPhilipps is an unknown quantity at this point 
Solved Threads: 0
JohnPhilipps JohnPhilipps is offline Offline
Light Poster

Re: populate array list from text file

 
0
  #5
Jun 8th, 2009
Thank you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC