音乐管理类:
import java.io.File;
/**
* 我的音乐管理
* @author tntxia
*
*/
public class MusicManager {
/**
* 音乐文件转移
* @param kugouPath
* @param musicPath
*/
public static void transMusic(String kugouPath,String musicPath){
File kugouPathFile = new File(kugouPath);
// 列出Kugou文件夹中所有的文件
for(File f : kugouPathFile.listFiles()){
if(f.isFile()){
String fileName = f.getName(); // 获取文件名
String singer = fileName.split("-")[0].trim(); // 从文件名获得歌手的名字
File file = new File(musicPath+"\\"+singer); // 创建歌手的文件夹
if(!file.exists()) // 如果文件夹不存在,新建这个文件夹
file.mkdir();
f.renameTo(new File(musicPath+"\\"+singer+"\\"+fileName)); // 把文件转移这个文件夹
}
}
}
}
公共窗口类
import java.awt.Container;
import java.util.Map;
import javax.swing.JFrame;
/**
*
* Swing公共窗口类
* @author tntxia
*
*/
public class CommonFrame extends JFrame{
private Container content = null;
public CommonFrame(){
this.content = this.getContentPane();
// 关闭自动退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Container getContent() {
return content;
}
public void setContent(Container content) {
this.content = content;
}
}
音乐管理窗口类
import java.awt.*;
import java.awt.event.*;
import java.util.ResourceBundle;
import javax.swing.*;
/**
* 音乐管理可视化窗口
* @author tntxia
*
*/
public class MusicFrame extends CommonFrame{
private JLabel label = new JLabel("Kugou目录:");
private JTextField text = new JTextField(20);
private JLabel label2 = new JLabel("音乐目录:");
private JTextField text2 = new JTextField(20);
private JButton button = new JButton("转换");
public MusicFrame(){
ResourceBundle rs = ResourceBundle.getBundle("music");
Container content = this.getContent();
content.setLayout(new FlowLayout());
content.add(label);
text.setText(rs.getString("kugouPath"));
content.add(text);
content.add(label2);
text2.setText(rs.getString("musicPath"));
content.add(text2);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
MusicManager.transMusic(text.getText(), text2.getText());
}
});
content.add(button);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
new MusicFrame();
}
}