简体   繁体   English

使用JFileChooser将JTextArea中的某些文本保存到文件中

[英]Saving certain text from JTextArea to a file using JFileChooser

I have this text from my JTextArea : 我的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 . 使用上面的代码,它将保存JTextArea所有JTextArea Can you help me? 你能帮助我吗?

PS If possible, I want to save it as an M3U Playlist. PS:如果可能,我想将其另存为M3U播放列表。

I'm assuming searchMP3Results is the JTextArea containing the text. 我假设searchMP3Results是包含文本的JTextArea 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. 在这种情况下,您可以使用searchMP3Results.getText()将文本作为String获取,然后通过正则表达式运行结果以查找文件路径。 An example regex for Windows paths is on this question java regular expression to match file path . Windows路径的正则表达式示例在此问题上是Java正则表达式以匹配文件路径 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. 不幸的是,这将您的应用程序绑定到Windows,但是如果可以接受,那么您就可以使用了,否则您应该使用系统属性检测操作系统并选择正确的正则表达式。

As far as the m3u you should just be able to export the directory paths (one per line). 至于m3u,您应该只能够导出目录路径(每行一个)。 Extended m3u files (using the header #EXTM3U ) require additional information, but you should be able to get away with the simple version. 扩展的m3u文件(使用标题#EXTM3U )需要其他信息,但是您应该能够摆脱简单版本的#EXTM3U

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 更新:添加了代码更新2:将正则表达式更改为路径正则表达式(副文件)的修改版本,现在针对每行运行它,而不是执行多行评估

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. 这段代码将输入分割成一个行数组(您可能要使用\\r\\n而不是仅使用\\n ),然后使用正则表达式检查该行是否为路径/文件名组合。 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. 我的意思是正则表达式不检查路径/文件名中的无效字符,也不检查文件的存在,尽管这很容易添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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