1

I have this text from my JTextArea:

Getting all .mp3 files in C:\Users\Admin\Music including those in subdirectories

C:\Users\Admin\Music\Sample Music\Kalimba.mp3
C:\Users\Admin\Music\Sample Music\Maid with the Flaxen Hair.mp3
C:\Users\Admin\Music\Sample Music\Sleep Away.mp3

Finished Searching...

I want to save only this part:

C:\Users\Admin\Music\Sample Music\Kalimba.mp3
C:\Users\Admin\Music\Sample Music\Maid with the Flaxen Hair.mp3
C:\Users\Admin\Music\Sample Music\Sleep Away.mp3

Unfortunately I can't with the code below:

JFileChooser saveFile = new JFileChooser("./");  
int returnVal = saveFile.showSaveDialog(this);  
File file = saveFile.getSelectedFile();  
BufferedWriter writer = null;  
if (returnVal == JFileChooser.APPROVE_OPTION)  
{  
    try {  
    writer = new BufferedWriter( new FileWriter( file.getAbsolutePath()+".txt")); // txt for now but needs to be m3u 
    searchMP3Results.write(writer); // using JTextArea built-in writer
    writer.close( );  
    JOptionPane.showMessageDialog(this, "Search results have been saved!",  
                "Success", JOptionPane.INFORMATION_MESSAGE);  
    }  
    catch (IOException e) {  
    JOptionPane.showMessageDialog(this, "An error has occured",  
                "Failed", JOptionPane.INFORMATION_MESSAGE);  
    }  
}

With the code above, it saves everything from the JTextArea. Can you help me?

P.S. If possible, I want to save it as an M3U Playlist.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Darwish
  • 37
  • 1
  • 7
  • I recommend putting the message `Getting all .mp3 files in C:\Users\Admin\Music including those in subdirectories` in a `JLabel` instead. But note that a file list is not an M3U file (AFAIU). You may have software clever enough to consider a file list to be an M3U file, but that does not make it a valid M3U file. E.G. an M3U file must start with first line as `#EXTM3U`, while the lines like `#EXTINF:270,10CC - Dreadlock Holiday` are optional, but highly useful. – Andrew Thompson Jan 06 '14 at 04:03
  • can I just use indexOf() to get certain parts and then add the rest manually i.e. #EXTINF etc? – Darwish Jan 06 '14 at 04:08
  • Well, not using the `Writer`, unless of course you use a second, non-visible text area, dump the M3U form of the data to it, and use that to write the data instead. BTW - I vaguely recall an M3U should be a particular encoding. Make sure the `FileWriter` accounts for that. – Andrew Thompson Jan 06 '14 at 04:11

1 Answers1

0

I'm assuming searchMP3Results is the JTextArea containing the text. In this case you could just get the text as a String using searchMP3Results.getText() and run the result through a regular expression looking for file paths. An example regex for Windows paths is on this question java regular expression to match file path. Unfortunately this ties your application to Windows, but if that's acceptable then you're good to go otherwise you should detect the OS using system properties and select the correct regex.

As far as the m3u you should just be able to export the directory paths (one per line). Extended m3u files (using the header #EXTM3U) require additional information, but you should be able to get away with the simple version.

Update: Added code Update 2: Changed regex to a modified version of path regex (vice file) and now run it against each line instead of performing a multiline assessment

String text = searchMP3Results.getText();
StringBuilder output = new StringBuilder();
for ( String s : text.split("\n") ) {
    if ( java.util.regex.Pattern.matches("^([a-zA-Z]:)?(\\\\[\\s\\.a-zA-Z0-9_-]+)+\\\\?$", s) ) {
        output.append(s).append("\n");
    }
}

This code splits the input into an array of lines (you may want to use \r\n instead of just \n) and then uses a regex to check if the line is a path/filename combination. No further checks are performed and the path/filename is assumed to be valid since it's presumably coming from an external application. What I mean is the regex doesn't check for invalid characters in the path/filename nor does it check for the file existence though this would be trivial to add.

Community
  • 1
  • 1
disrvptor
  • 1,592
  • 12
  • 23