Results 1 to 5 of 5
  1. #1
    GraemeH is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

  4. #4
    GraemeH is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks pal thats helped me get started.

  5. #5
    GraemeH is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    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:

    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");  
       		}
       		
        }
      }
    }
    The program reads a file for what i want and outputs it.
    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.
    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();
                }
            });
        }
    }
    but i just cant put the two programs together so the the GUI file chooser reads the RegEx expressions and outputs the results.

    could anyone share any advice on how i would do this please?


    Thanks
    G

Similar Threads

  1. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  2. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  3. Replies: 4
    Last Post: 07-07-2008, 01:32 PM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  5. read txt file
    By sureshsri1981 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-05-2007, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •