Results 1 to 5 of 5
Thread: Read file delimiters
- 03-18-2009, 02:25 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
Read file delimiters
Hello everyone,
I am trying to make a Java program to read a .dat file for various items of data. The .dat file is jumbled up with whitespace and random characters. The information I need is dotted about within this.
An example of the data i would like to retrieve from the .dat file:
fednjk<4893ujfoief><audio title="Bittersweet Symphony" artist="The Verve" album="This Is Music: The Singles 92-98" genre="Alternative" track="12" year="2004" seconds="356" bitrate="128"/>fdaga<dfsgfgdg>
However there are lots of whitespaces and random characters before and after the example above. My aim is to retrieve the audio title, artist, album, genre, track and year of the download. I don’t know how to go about this. This is just an example of part of the file, there are other song downloads within the file and I want to retrieve the data for all of them.
I was thinking I’d need to read the file until EOF, looking for a start delimiter for each field along the way and read what is after it, until the end delimiter, e.g audio title=" read what ever is inbetween here ". Then continue doing this for artist=" read what ever is inbetween here". Or, would I just use " " to separate all the data?
I really don’t have a clue how to go about this. Do I use split() to do this, or StringTokenizer or StreamTokenizer or something else? And what would I use to read the file?
Any help at all would be much appreciated I just don’t know which direction to go in.
P.S I hope this has been posted in the right forum! Appoligies if it hasnt.
Thanks
Graeme
- 03-18-2009, 02:33 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
It looks as though it is suppossed to be XML. Use an XMLParser (and the JDK contains two, a DOM and a SAX XML Parser) and search out the "audio" tags.
- 03-18-2009, 02:33 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
If you insist, however Regular Expression Tutorial - Learn How to Use Regular Expressions
- 03-18-2009, 03:22 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
thanks pal thats helped me get started.
- 03-29-2009, 11:44 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
Hello again everyone.
i've managed to get the issue sorted using RegEx after your suggestions thanks for that.
the code for that is below:
The program reads a file for what i want and outputs it.Java Code:import java.util.regex.*; import java.io.*; public class LimeWireAnalyserDownloadDat { public static void main(String[] args) throws Exception { String regex1 = "(?s)C:(.*?)w"; String regex2 = "(?s)titlet(.*?)t"; Pattern pattern1 = Pattern.compile(regex1); Pattern pattern2 = Pattern.compile(regex2); BufferedReader br = new BufferedReader (new FileReader("C://Users//Mike//Documents//LimeWire//Incomplete//downloads.dat")); String text; while ((text = br.readLine()) != null) { Matcher matcher1 = pattern1.matcher(text); while (matcher1.find()) { System.out.println("Downloaded File and Location: \n" + matcher1.group() + "\n"); } Matcher matcher2 = pattern2.matcher(text); while (matcher2.find()) { System.out.println("Search Term: \n" + matcher2.group() + "\n"); } } } }
My problem now is putting that into a GUI and letting the user choose which file they want (as opposed to one ive hard coded) and output the result in a GUI rather than the system.out.println.
Below i've got a GUI to read a file and output it.
but i just cant put the two programs together so the the GUI file chooser reads the RegEx expressions and outputs the results.Java Code:package components; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.SwingUtilities; import javax.swing.filechooser.*; public class FileChooser extends JPanel implements ActionListener { JButton openButton; JButton saveButton; JButton printButton; JTextArea log; JFileChooser fc; public FileChooser() //Points to the user's default directory { super(new BorderLayout()); //Postions border layout //Create the log first, for the action listeners to refer to log = new JTextArea(15,75); log.setMargin(new Insets(5,5,5,5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); //Create a file chooser fc = new JFileChooser(); //Create the open button openButton = new JButton("Open File"); openButton.addActionListener(this); //Create the save button saveButton = new JButton("Save As"); saveButton.addActionListener(this); //Create the print button printButton = new JButton("Print"); printButton.addActionListener(this); //Put the buttons in a separate panel JPanel buttonPanel = new JPanel(); buttonPanel.add(openButton); buttonPanel.add(saveButton); buttonPanel.add(printButton); //Add the buttons and the log to this panel. add(buttonPanel, BorderLayout.PAGE_START); add(logScrollPane, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { //Handle open button action if (e.getSource() == openButton) { int returnVal = fc.showOpenDialog(FileChooser.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // while not at end of file read file, // if equal to pre fixed term then print out, if not carry on reading file // dis.available() returns 0 if the file does not have more lines while (dis.available() != 0) { // this statement reads the line from the file and print it to the GUI log.append(dis.readLine().trim() + "\n"); } // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } log.append("\n" + "File Opened: " + file.getName() + "." + "\n"); } else { log.append("Open command cancelled by user." + "\n"); } log.setCaretPosition(log.getDocument().getLength()); } //Handle save button action else if (e.getSource() == saveButton) { int returnVal = fc.showSaveDialog(FileChooser.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would save the file log.append("Saving: " + file.getName() + "." + "\n"); } else { log.append("Save command cancelled by user." + "\n"); } log.setCaretPosition(log.getDocument().getLength()); } } // Create the GUI and display it private static void createAndDisplayGUI() { //Create and set up the window JFrame frame = new JFrame("LimeWire Analyser v1.0"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add content to the window frame.add(new FileChooser()); //Display the window frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Creating and show the application's GUI SwingUtilities.invokeLater(new Runnable() { public void run() { createAndDisplayGUI(); } }); } }
could anyone share any advice on how i would do this please?
Thanks
G
Similar Threads
-
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
Extracting words from a string using delimiters
By toad in forum New To JavaReplies: 4Last Post: 07-07-2008, 01:32 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
read txt file
By sureshsri1981 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-05-2007, 03:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks