- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 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...
我只想保存这部分:
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
不幸的是我不能使用下面的代码:
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);
}
}
使用上面的代码,它保存了 JTextArea
中的所有内容。你能帮我吗?
附注如果可能的话,我想将其保存为 M3U 播放列表。
最佳答案
我假设 searchMP3Results
是包含文本的 JTextArea
。在这种情况下,您可以使用 searchMP3Results.getText()
以 String
形式获取文本,并通过查找文件路径的正则表达式运行结果。 Windows 路径的正则表达式示例位于此问题 java regular expression to match file path 上。 。不幸的是,这将您的应用程序与 Windows 联系在一起,但如果这是可以接受的,那么您就可以开始了,否则您应该使用系统属性检测操作系统并选择正确的正则表达式。
就 m3u 而言,您应该只能导出目录路径(每行一个)。扩展 m3u 文件(使用 header #EXTM3U
)需要其他信息,但您应该能够使用简单版本。
更新:添加代码更新 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");
}
}
此代码将输入拆分为行数组(您可能需要使用 \r\n
而不仅仅是 \n
),然后使用正则表达式来检查如果该行是路径/文件名组合。不执行进一步的检查,并且路径/文件名被假定为有效,因为它可能来自外部应用程序。我的意思是正则表达式不会检查路径/文件名中的无效字符,也不会检查文件是否存在,尽管添加起来很简单。
关于java - 使用 JFileChooser 将某些文本从 JTextArea 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942757/
我有一个简单的 GUI,其中有一个 JTextArea。我创建了一个方法来从用户获取消息,另一个方法将文本附加到文本区域,如下所示 Message m = new Message(); ... pri
我正在使用 JList,并且尝试对单元格使用 JTextAreas(实现 ListCellRenderer)。它不起作用。这些单元格仅显示 ListCellRenderer.toString() 而不
此代码计算 JTextArea 的每一行并添加行数 左 JTextPane import java.awt.BorderLayout; import java.awt.Color; import ja
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我必须将一个jtextarea的内容复制到另一个jtextarea。怎么办?我已经做了以下操作:但是这个程序正在将一个jtext区域的文本逐个字符写入另一个jtext区域。我希望它在用户按下回车键时复
在我的 GUI 中,JScrollPane 中有一个附加到容器的 JTextArea。 ta = new JTextArea(); jsp = new JScrollP
我正在编写一些测试代码来练习 OOP,我想将 JTextArea 从“writeToArea”附加到定义和初始化 JTextArea 的“initialize”方法。我已经尝试直接调用“输出”变量,但
JComboBox cBox = new JComboBox(); cBox.addItem("Food"); String x = "Food"; cBox.addItem("Shi
我正在尝试将一个 JTextArea 放在 GUI 中的另一个 JTextArea 旁边 我正在为数据库编写 GUI,并希望将每列的数据放在不同的 JTextArea 中。这将使我的 GUI 看起来更
这是一个带有 JTextarea 的弹出 Jpanel,但我有一个问题。当我将鼠标移到 JTextarea 上时,它会闪烁。为什么会出现这种情况? 在 Debug模式下,鼠标移动会生成 mouseEx
我有一个类将输出显示到 JTextArea 中。意味着成功运行后,它将在文本区域中显示输出。 我还有一个主类,它将类与几个按钮组合在一起,以启动特定类中代码的执行。这个主类创建了一个带有几个按钮的 G
我的问题在于我的 DocumentLister AreaListener。我似乎无法弄清楚如何将用户输入的文本传递到一个 JTextArea 中进行转换,并将其返回到另一个 JTextArea。 该程
我有一个对象ReminderGUI其中有 JTextArea field 。 ReminderGUI代表一个可以保存和显示提醒的应用程序。当getReminderButton单击我希望应用程序找到之前
我目前正在使用 Swing 开发控制台窗口。它基于 JTextArea 并且像普通命令行一样工作。您在一行中键入一条命令,然后按回车键。在下一行中,显示了输出,在该输出下,您可以编写下一条命令。 现在
我开发了一个 Swing GUI,其中我尝试使用按钮使用另一个文本区域中的文本填充文本区域。 代码: private void jButton1ActionPerformed(java.awt.eve
当我制作时,我有一个包含 JPanel 的小型 GUI,其中有 JTextArea 和 JLabel panel1.setLayout(null); 我可以完成所需的位置,但 JTextArea 消失
我在 JDialog 框中有一个 JTabbedPane,它在 Pane 中包含的所有 JPanels 上使用 GridBagLayout 。在显示的第一个面板上有一个 JTextArea (desc
我搜索了答案,但我找到的只是解决方法,而不是原因,所以我问这个问题: 我是 GUI 编程的新手。在练习一些有关关键事件处理的代码时,我遇到了一个示例,该示例在 JFrame 中包含一个 JTextAr
将有 91 个文本区域,每个区域都显示一个值。我试图找到一种更有效的方法来解决这个问题,而不是只是盲目地添加 JTextAreas 并尝试管理 91 个文本区域中每个区域的实例化名称。 我愿意接受有关
private JPanel contentPane; public Driver() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我是一名优秀的程序员,十分优秀!