简体   繁体   中英

Saving certain text from JTextArea to a file using JFileChooser

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM