| | |
populate array list from text file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 27
Reputation:
Solved Threads: 0
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:
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
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)
import java.io.*; class FileReadTest { public static void main (String[] args) { FileReadTest f = new FileReadTest(); f.readMyFile(); } @SuppressWarnings("deprecation") void readMyFile() { DataInputStream dis = null; String record = null; int recCount = 0; try { File f = new File("mydata.txt"); FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while ( (record=dis.readLine()) != null ) { recCount++; System.out.println(recCount + ": " + record); } } catch (IOException e) { // catch io errors from FileInputStream or readLine() System.out.println("IOException error!" + e.getMessage()); } finally { // if the file opened okay, make sure we close it if (dis != null) { try { dis.close(); } catch (IOException ioe) { } } } } }
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
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()
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
![]() |
Similar Threads
- Need help comparing array to a string from text file. (C++)
- Extract part of text file into 2 dimensional string array (Java)
- Writing and reading array to/from text file (C++)
- delete a list in text file (Python)
- problems editing dataGrid and 'Tab' delimited text file (C#)
- How to copy floating point values from text file to array (C)
- Help: importing text file to a word doc using array (Java)
- Homework: filling array from text file (VB.NET)
- Inputting text file data into an array, please help! (C++)
Other Threads in the Java Forum
- Previous Thread: Calculator (value of an arithmetic expression)
- Next Thread: String input
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






