论坛首页 Java企业应用论坛

简易的mp3分拣程序

浏览 5726 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-23   最后修改:2010-04-23

硬盘内的mp3文件太多了,下的时候没有归类,结果比较乱,就自己写了个小程序分拣.上代码:

 

程序运行图

 

会将文件按照这两种格式放置在选定的目录中

 

主程序

 

package com.javaeye.i2534;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

/**
 * 
 * @author lan
 * 
 */
public class MP3Manager extends JFrame {

	private static final long	serialVersionUID	= 1L;
	private JPanel				jContentPane		= null;
	private JPanel				jPanel				= null;
	private JLabel				pathLabel			= null;
	private JTextField			pathTextField		= null;
	private JButton				browseButton		= null;
	private JScrollPane			jScrollPane			= null;
	private JPanel				jPanel1				= null;
	private JList				fileList			= null;
	private JLabel				nameLabel			= null;
	private JLabel				artistLabel			= null;
	private JLabel				albumLabel			= null;
	private JLabel				yearLabel			= null;
	private JLabel				commentLabel		= null;
	private JTextField			nameTextField		= null;
	private JTextField			artistTextField		= null;
	private JTextField			albumTextField		= null;
	private JTextField			yearTextField		= null;
	private JTextArea			commentTextArea		= null;
	private JMenuBar			mainMenuBar			= null;
	private JMenu				fileMenu			= null;
	private JMenuItem			exitMenuItem		= null;
	private JMenuItem			groupMenuItem		= null;

	/**
	 * This is the default constructor
	 */
	public MP3Manager() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar(getMainMenuBar());
		this.setSize(560, 415);
		this.add(getJContentPane(), BorderLayout.CENTER);
		this.setTitle("MP3管理工具");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			pathLabel = new JLabel();
			pathLabel.setBounds(new Rectangle(36, 21, 54, 24));
			pathLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			pathLabel.setText("文件夹");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(pathLabel, null);
			jPanel.add(getPathTextField(), null);
			jPanel.add(getBrowseButton(), null);
			jPanel.add(getJScrollPane(), null);
			jPanel.add(getJPanel1(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes pathTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPathTextField() {
		if (pathTextField == null) {
			pathTextField = new JTextField();
			pathTextField.setBounds(new Rectangle(112, 20, 311, 26));
		}
		return pathTextField;
	}

	/**
	 * This method initializes browseButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBrowseButton() {
		if (browseButton == null) {
			browseButton = new JButton();
			browseButton.setBounds(new Rectangle(438, 21, 62, 22));
			browseButton.setText("浏览");
			browseButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser chooser = new JFileChooser(FileSystemView
							.getFileSystemView());
					chooser
							.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
					chooser.addChoosableFileFilter(new FileFilter() {

						@Override
						public boolean accept(File f) {
							if (f.isDirectory()) {
								return true;
							} else if (f.isFile()) {
								return f.getName().toLowerCase()
										.endsWith(".mp3");
							}
							return false;
						}

						@Override
						public String getDescription() {
							return "MP3 (.mp3)";
						}

					});
					if (chooser.showOpenDialog(MP3Manager.this) == JFileChooser.APPROVE_OPTION) {
						File dir = chooser.getSelectedFile();
						getPathTextField().setText(dir.getAbsolutePath());
						Set<File> set = new HashSet<File>();
						MP3Util.walk(dir, set);
						DefaultListModel model = (DefaultListModel) getFileList()
								.getModel();
						model.clear();
						List<FileWrapper> warppers = new ArrayList<FileWrapper>();
						for (File f : set) {
							warppers.add(new FileWrapper(f));
						}
						Collections.sort(warppers);
						for (FileWrapper f : warppers) {
							model.addElement(f);
						}
					}
				}
			});
		}
		return browseButton;
	}

	/**
	 * file包装器,方便list显示
	 * 
	 * @author lan
	 * 
	 */
	private class FileWrapper implements Comparable<FileWrapper> {
		File	file;

		public FileWrapper(File file) {
			this.file = file;
		}

		@Override
		public String toString() {
			return file.getName();
		}

		public int compareTo(FileWrapper o) {
			if (o == null) {
				return -1;
			}
			return this.file.getName().compareTo(o.file.getName());
		}
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(23, 75, 197, 265));
			jScrollPane.setViewportView(getFileList());
			jScrollPane
					.setBorder(BorderFactory
							.createTitledBorder(null, "MP3\u5217\u8868", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font(
									"Dialog", Font.BOLD, 12), new Color(51, 51,
									51)));
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			commentLabel = new JLabel();
			commentLabel.setBounds(new Rectangle(25, 190, 50, 20));
			commentLabel.setText("信息");
			yearLabel = new JLabel();
			yearLabel.setBounds(new Rectangle(25, 150, 50, 20));
			yearLabel.setText("年代");
			albumLabel = new JLabel();
			albumLabel.setBounds(new Rectangle(25, 110, 50, 20));
			albumLabel.setText("专辑");
			artistLabel = new JLabel();
			artistLabel.setBounds(new Rectangle(25, 70, 50, 20));
			artistLabel.setText("演唱");
			nameLabel = new JLabel();
			nameLabel.setBounds(new Rectangle(25, 30, 50, 20));
			nameLabel.setText("名称");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(257, 75, 256, 264));
			jPanel1
					.setBorder(BorderFactory
							.createTitledBorder(null, "MP3\u4fe1\u606f", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font(
									"Dialog", Font.BOLD, 12), new Color(51, 51,
									51)));
			jPanel1.add(nameLabel, null);
			jPanel1.add(artistLabel, null);
			jPanel1.add(albumLabel, null);
			jPanel1.add(yearLabel, null);
			jPanel1.add(commentLabel, null);
			jPanel1.add(getNameTextField(), null);
			jPanel1.add(getArtistTextField(), null);
			jPanel1.add(getAlbumTextField(), null);
			jPanel1.add(getYearTextField(), null);
			jPanel1.add(getCommentTextArea(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes fileList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getFileList() {
		if (fileList == null) {
			fileList = new JList(new DefaultListModel());
			fileList
					.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
						public void valueChanged(
								javax.swing.event.ListSelectionEvent e) {
							if (e.getValueIsAdjusting()) {
								Object o = fileList.getSelectedValue();
								if (o instanceof FileWrapper) {
									File f = ((FileWrapper) o).file;
									MP3Info info = MP3Util.getInfo(f);
									getNameTextField().setText(info.getName());
									getArtistTextField().setText(info
											.getArtist());
									getAlbumTextField()
											.setText(info.getAlbum());
									getYearTextField().setText(info.getYear());
									getCommentTextArea().setText(info
											.getComment());
								}
							}
						}
					});
		}
		return fileList;
	}

	/**
	 * This method initializes nameTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setBounds(new Rectangle(95, 30, 130, 20));
			nameTextField.setEditable(false);
		}
		return nameTextField;
	}

	/**
	 * This method initializes artistTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getArtistTextField() {
		if (artistTextField == null) {
			artistTextField = new JTextField();
			artistTextField.setBounds(new Rectangle(95, 70, 130, 20));
			artistTextField.setEditable(false);
		}
		return artistTextField;
	}

	/**
	 * This method initializes albumTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getAlbumTextField() {
		if (albumTextField == null) {
			albumTextField = new JTextField();
			albumTextField.setBounds(new Rectangle(95, 110, 130, 20));
			albumTextField.setEditable(false);
		}
		return albumTextField;
	}

	/**
	 * This method initializes yearTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getYearTextField() {
		if (yearTextField == null) {
			yearTextField = new JTextField();
			yearTextField.setBounds(new Rectangle(95, 150, 130, 20));
			yearTextField.setEditable(false);
		}
		return yearTextField;
	}

	/**
	 * This method initializes commentTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getCommentTextArea() {
		if (commentTextArea == null) {
			commentTextArea = new JTextArea();
			commentTextArea.setBounds(new Rectangle(95, 190, 130, 60));
			commentTextArea.setEditable(false);
			commentTextArea.setEnabled(true);
			commentTextArea.setLineWrap(true);
		}
		return commentTextArea;
	}

	/**
	 * This method initializes mainMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new JMenuBar();
			mainMenuBar.add(getFileMenu());
		}
		return mainMenuBar;
	}

	/**
	 * This method initializes fileMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setPreferredSize(new Dimension(50, 20));
			fileMenu.setText("文件");
			fileMenu.setBounds(new Rectangle(0, 0, 50, 20));
			fileMenu.add(getGroupMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes exitMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("退出");
			exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes groupMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getGroupMenuItem() {
		if (groupMenuItem == null) {
			groupMenuItem = new JMenuItem();
			groupMenuItem.setText("分组");
			groupMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String text = getPathTextField().getText();
							File dir = new File(text);
							if (dir.exists() && dir.isDirectory()) {
								MP3GroupDialog sd = new MP3GroupDialog(
										MP3Manager.this);
								sd.setPath(dir);
								sd.setVisible(true);
							}
						}
					});
		}
		return groupMenuItem;
	}

	public static void main(String[] args) {
		MP3Manager m = new MP3Manager();
		m.setLocationRelativeTo(null);
		m.setVisible(true);
	}

} // @jve:decl-index=0:visual-constraint="10,10"

 

MP3的info类

 

package com.javaeye.i2534;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * mp3信息,遵循ID3v1格式
 * 
 * @author lan
 * 
 */
public class MP3Info {

	private final Charset	GBK	= Charset.forName("GBK");
	// 应该是128位
	private byte[]			tag;

	public MP3Info(byte[] tag) throws IllegalArgumentException {
		this.tag = tag;
		if (this.tag.length != 128) {
			throw new IllegalArgumentException();
		}
		if (!isValid()) {
			throw new IllegalArgumentException();
		}
	}

	/**
	 * 是否包含TAG标签,如果没有,就是不合法的信息,不能读取
	 * 
	 * @return
	 */
	private boolean isValid() {
		byte[] array = Arrays.copyOfRange(this.tag, 0, 3);
		String s = new String(array, GBK);
		return "TAG".equals(s);
	}

	/**
	 * 艺术家
	 * 
	 * @return
	 */
	public String getArtist() {
		return getString(33, 63);
	}

	/**
	 * 专辑
	 * 
	 * @return
	 */
	public String getAlbum() {
		return getString(63, 93);
	}

	/**
	 * 音乐名称
	 * 
	 * @return
	 */
	public String getName() {
		return getString(3, 33);
	}

	/**
	 * 年代
	 * 
	 * @return
	 */
	public String getYear() {
		return getString(93, 97);
	}

	/**
	 * 备注信息
	 * 
	 * @return
	 */
	public String getComment() {
		return getString(97, 125);
	}

	/**
	 * 流派
	 */
	public String getCenre() {
		return getString(127, 128);
	}

	/**
	 * 获取tag中指定范围内的字符串
	 * 
	 * @param from
	 * @param to
	 * @return
	 */
	private String getString(int from, int to) {
		byte[] array = Arrays.copyOfRange(this.tag, from, to);
		int length = 0;
		for (byte b : array) {
			if (b != 0) {
				length++;
			} else {
				break;
			}
		}
		return new String(array, 0, length, GBK);
	}

	/*
	 * ID3 GENRES LIST
	 * 
	 * 0 - Blues
	 * 
	 * 1 - Classic Rock
	 * 
	 * 2 - Country
	 * 
	 * 3 - Dance
	 * 
	 * 4 - Disco
	 * 
	 * 5 - Funk
	 * 
	 * 6 - Grunge
	 * 
	 * 7 - Hip-Hop
	 * 
	 * 8 - Jazz
	 * 
	 * 9 - Metal
	 * 
	 * 10 - New Age
	 * 
	 * 11 - Oldies
	 * 
	 * 12 - Other
	 * 
	 * 13 - Pop
	 * 
	 * 14 - R&B
	 * 
	 * 15 - Rap
	 * 
	 * 16 - Reggae
	 * 
	 * 17 - Rock
	 * 
	 * 18 - Techno
	 * 
	 * 19 - Industrial
	 * 
	 * 20 - Alternative
	 * 
	 * 21 - Ska
	 * 
	 * 22 - Death Metal
	 * 
	 * 23 - Pranks
	 * 
	 * 24 - Soundtrack
	 * 
	 * 25 - Euro-Techno
	 * 
	 * 26 - Ambient
	 * 
	 * 27 - Trip-Hop
	 * 
	 * 28 - Vocal
	 * 
	 * 29 - Jazz+Funk
	 * 
	 * 30 - Fusion
	 * 
	 * 31 - Trance
	 * 
	 * 32 - Classical
	 * 
	 * 33 - Instrumental
	 * 
	 * 34 - Acid
	 * 
	 * 35 - House
	 * 
	 * 36 - Game
	 * 
	 * 37 - Sound Clip
	 * 
	 * 38 - Gospel
	 * 
	 * 39 - Noise
	 * 
	 * 40 - Alternative Rock
	 * 
	 * 41 - Bass
	 * 
	 * 42 - Soul
	 * 
	 * 43 - Punk
	 * 
	 * 44 - Space
	 * 
	 * 45 - Meditative
	 * 
	 * 46 - Instrumental Pop
	 * 
	 * 47 - Instrumental Rock
	 * 
	 * 48 - Ethnic
	 * 
	 * 49 - Gothic
	 * 
	 * 50 - Darkwave
	 * 
	 * 51 - Techno-Industrial
	 * 
	 * 52 - Electronic
	 * 
	 * 53 - Pop-Folk
	 * 
	 * 54 - Eurodance
	 * 
	 * 55 - Dream
	 * 
	 * 56 - Southern Rock
	 * 
	 * 57 - Comedy
	 * 
	 * 58 - Cult
	 * 
	 * 59 - Gangsta
	 * 
	 * 60 - Top 40
	 * 
	 * 61 - Christian Rap
	 * 
	 * 62 - Pop/Funk
	 * 
	 * 63 - Jungle
	 * 
	 * 64 - Native US
	 * 
	 * 65 - Cabaret
	 * 
	 * 66 - New Wave
	 * 
	 * 67 - Psychadelic
	 * 
	 * 68 - Rave
	 * 
	 * 69 - Showtunes
	 * 
	 * 70 - Trailer
	 * 
	 * 71 - Lo-Fi
	 * 
	 * 72 - Tribal
	 * 
	 * 73 - Acid Punk
	 * 
	 * 74 - Acid Jazz
	 * 
	 * 75 - Polka
	 * 
	 * 76 - Retro
	 * 
	 * 77 - Musical
	 * 
	 * 78 - Rock & Roll
	 * 
	 * 79 - Hard Rock
	 * 
	 * 80 - Folk
	 * 
	 * 81 - Folk-Rock
	 * 
	 * 82 - National Folk
	 * 
	 * 83 - Swing
	 * 
	 * 84 - Fast Fusion
	 * 
	 * 85 - Bebob
	 * 
	 * 86 - Latin
	 * 
	 * 87 - Revival
	 * 
	 * 88 - Celtic
	 * 
	 * 89 - Bluegrass
	 * 
	 * 90 - Avantgarde
	 * 
	 * 91 - Gothic Rock
	 * 
	 * 92 - Progressive Rock
	 * 
	 * 93 - Psychedelic Rock
	 * 
	 * 94 - Symphonic Rock
	 * 
	 * 95 - Slow Rock
	 * 
	 * 96 - Big Band
	 * 
	 * 97 - Chorus
	 * 
	 * 98 - Easy Listening
	 * 
	 * 99 - Acoustic
	 * 
	 * 100 - Humour
	 * 
	 * 101 - Speech
	 * 
	 * 102 - Chanson
	 * 
	 * 103 - Opera
	 * 
	 * 104 - Chamber Music
	 * 
	 * 105 - Sonata
	 * 
	 * 106 - Symphony
	 * 
	 * 107 - Booty Bass
	 * 
	 * 108 - Primus
	 * 
	 * 109 - Porn Groove
	 * 
	 * 110 - Satire
	 * 
	 * 111 - Slow Jam
	 * 
	 * 112 - Club
	 * 
	 * 113 - Tango
	 * 
	 * 114 - Samba
	 * 
	 * 115 - Folklore
	 * 
	 * 116 - Ballad
	 * 
	 * 117 - Power Ballad
	 * 
	 * 118 - Rhythmic Soul
	 * 
	 * 119 - Freestyle
	 * 
	 * 120 - Duet
	 * 
	 * 121 - Punk Rock
	 * 
	 * 122 - Drum Solo
	 * 
	 * 123 - Acapella
	 * 
	 * 124 - Euro-House
	 * 
	 * 125 - Dance Hall
	 * 
	 * 126 - Goa
	 * 
	 * 127 - Drum & Bass
	 * 
	 * 128 - Club - House
	 * 
	 * 129 - Hardcore
	 * 
	 * 130 - Terror
	 * 
	 * 131 - Indie
	 * 
	 * 132 - BritPop
	 * 
	 * 133 - Negerpunk
	 * 
	 * 134 - Polsk Punk
	 * 
	 * 135 - Beat
	 * 
	 * 136 - Christian Gangsta Rap
	 * 
	 * 137 - Heavy Metal
	 * 
	 * 138 - Black Metal
	 * 
	 * 139 - Crossover
	 * 
	 * 140 - Contemporary Christian
	 * 
	 * 141 - Christian Rock
	 * 
	 * 142 - Merengue
	 * 
	 * 143 - Salsa
	 * 
	 * 144 - Thrash Metal
	 * 
	 * 145 - Anime
	 * 
	 * 146 - JPop
	 * 
	 * 147 - Synthpop
	 * 
	 * 148 and further - Unknown
	 * 
	 * 255 Unknown
	 */
}

 

 

mp3分组的dialog类

 

package com.javaeye.i2534;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * 
 * @author lan
 *
 */
public class MP3GroupDialog extends JDialog {

	private static final long	serialVersionUID	= 1L;
	private JPanel				jContentPane		= null;
	private JPanel				jPanel				= null;
	private JRadioButton		jRadioButton		= null;
	private JRadioButton		jRadioButton1		= null;
	private JButton				jButton				= null;
	private JButton				jButton1			= null;
	private ButtonGroup			buttonGroup			= null;
	private File				path				= null; //@jve:decl-index=0:

	/**
	 * @param owner
	 */
	public MP3GroupDialog(Frame owner) {
		super(owner);
		initialize();
		this.setLocationRelativeTo(owner);
	}

	public void setPath(File path) {
		this.path = path;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(262, 246);
		this.setTitle("选择分组");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJRadioButton(), null);
			jPanel.add(getJRadioButton1(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setBounds(new Rectangle(50, 36, 141, 21));
			jRadioButton.setText("歌手-专辑-歌曲");
			jRadioButton.setActionCommand("aas");
			getButtonGroup().add(jRadioButton);
		}
		return jRadioButton;
	}

	/**
	 * This method initializes jRadioButton1
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(50, 80, 133, 21));
			jRadioButton1.setText("歌手-歌曲");
			jRadioButton1.setSelected(true);
			jRadioButton1.setActionCommand("as");
			getButtonGroup().add(jRadioButton1);
		}
		return jRadioButton1;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(38, 140, 60, 25));
			jButton.setText("确定");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Set<File> mp3s = new HashSet<File>();
					MP3Util.walk(path, mp3s);
					ButtonModel bm = getButtonGroup().getSelection();
					String command = bm.getActionCommand();
					if ("aas".equals(command)) {
						MP3GroupProgress progress = new MP3GroupProgress(null);
						progress.setVisible(true);// 不知道为什么,居然不显示
						int i = 0, length = mp3s.size();
						for (File f : mp3s) {
							i++;
							MP3Info info = MP3Util.getInfo(f);
							String artist = info.getArtist();
							if (artist == null) {
								artist = "未知歌手";
							}
							File dir = new File(path, artist);
							if (!dir.exists()) {
								dir.mkdirs();
							}
							String album = info.getAlbum();
							if (album == null) {
								album = "未知专辑";
							}
							File d = new File(dir, album);
							if (!d.exists()) {
								d.mkdirs();
							}
							String text = "";
							if (!f.getParentFile().equals(d)) {
								if (MP3Util.move(f, d)) {
									text = "移动文件 " + f.getName() + " 到 "
											+ d.getPath() + " 下";
								} else {
									text = "无法移动文件 " + f.getName();
								}
							} else {
								text = "文件 " + f.getName() + " 不需要移动";
							}
							System.out.println(text);
							progress.getJProgressBar().setString(text);
							progress.getJProgressBar()
									.setValue((int) (i * 100 / length));
						}
					} else if ("as".equals(command)) {
						MP3GroupProgress progress = new MP3GroupProgress(null);
						progress.setVisible(true);
						int i = 0, length = mp3s.size();
						for (File f : mp3s) {
							i++;
							MP3Info info = MP3Util.getInfo(f);
							String artist = info.getArtist();
							if (artist == null) {
								artist = "未知歌手";
							}
							File d = new File(path, artist);
							if (!d.exists()) {
								d.mkdirs();
							}
							String text = "";
							if (!f.getParentFile().equals(d)) {
								if (MP3Util.move(f, d)) {
									text = "移动文件 " + f.getName() + " 到 "
											+ d.getPath() + " 下";
								} else {
									text = "无法移动文件 " + f.getName();
								}
							} else {
								text = "文件 " + f.getName() + " 不需要移动";
							}
							System.out.println(text);
							progress.getJProgressBar().setString(text);
							progress.getJProgressBar()
									.setValue((int) (i * 100 / length));
						}
					}
					MP3GroupDialog.this.dispose();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(138, 140, 60, 25));
			jButton1.setText("取消");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					MP3GroupDialog.this.dispose();
				}
			});
		}
		return jButton1;
	}

	private ButtonGroup getButtonGroup() {
		if (buttonGroup == null) {
			buttonGroup = new ButtonGroup();
		}
		return buttonGroup;
	}

} // @jve:decl-index=0:visual-constraint="10,10"

 

 

然后是工具类

 

package com.javaeye.i2534;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Collection;

public final class MP3Util {

	private MP3Util() {};

	public static MP3Info getInfo(File file) {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(file, "r");
			byte[] array = new byte[128];
			raf.seek(raf.length() - 128);
			raf.read(array);
			MP3Info info = new MP3Info(array);
			return info;
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} finally {
			if (raf != null) {
				try {
					raf.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return null;
	}

	/**
	 * 遍历文件,获取所有后缀为mp3的文件
	 * 
	 * @param file
	 * @param files
	 */
	public static void walk(File file, Collection<File> files) {
		if (file.isHidden()) {
			return;
		}
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				MP3Util.walk(f, files);
			}
		} else if (file.isFile()) {
			if (file.getName().toLowerCase().endsWith(".mp3")) {
				files.add(file);
			}
		}
	}

	public static boolean move(File f, File dir) {
		if (f.exists() && f.isFile()) {
			try {
				RandomAccessFile raf = new RandomAccessFile(f, "r");
				FileChannel fc = raf.getChannel();
				RandomAccessFile wr = new RandomAccessFile(new File(dir, f
						.getName()), "rw");
				FileChannel wfc = wr.getChannel();
				fc.transferTo(0, fc.size(), wfc);
				wfc.close();
				raf.close();
				return f.delete();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}

 

 

最后是一个不能正常工作的进度条.....

 

package com.javaeye.i2534;

import java.awt.BorderLayout;
import java.awt.Cursor;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

public class MP3GroupProgress extends JWindow {

	private static final long	serialVersionUID	= 1L;
	private JPanel				jContentPane		= null;
	private JPanel				jPanel				= null;
	private JProgressBar		jProgressBar		= null;

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJProgressBar(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jProgressBar
	 * 
	 * @return javax.swing.JProgressBar
	 */
	public JProgressBar getJProgressBar() {
		if (jProgressBar == null) {
			jProgressBar = new JProgressBar(1, 100);
			jProgressBar.setStringPainted(true);
		}
		return jProgressBar;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final MP3GroupProgress p = new MP3GroupProgress(null);
		p.setVisible(true);
		new Thread() {
			public void run() {
				for (int i = 0; i < 100; i++) {
					p.getJProgressBar().setString("Now " + i);
					p.getJProgressBar().setValue(i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	/**
	 * @param owner
	 */
	public MP3GroupProgress(JDialog owner) {
		initialize();
		this.setLocationRelativeTo(owner);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(474, 45);
		this.setContentPane(getJContentPane());
		this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
  • src.zip (10.2 KB)
  • 下载次数: 246
   发表时间:2010-04-24  
正限机子上mp3也比较乱,顺便研究下你的代码。如果把歌曲名,专辑名等都做成参数的形式,那就可以自定义名字了,不限于你当前提供了两种。可以随意组合。
0 请登录后投票
   发表时间:2010-04-24  
看了看 只支持id3v1 现在大多数都是id3v2了 有的mp3甚至没有id3v1标签
0 请登录后投票
   发表时间:2010-04-24  
估计结果会有很多未分类的歌曲。
0 请登录后投票
   发表时间:2010-04-25  
唉 就是不老美观的 不过我也只是说说 我做的swing的东西比你的也好看不了
0 请登录后投票
   发表时间:2010-04-25  
问题不少阿,经常报异常!估计楼主还没有对mp3 分析透彻~ ,期待楼主进一步改进~
0 请登录后投票
   发表时间:2010-04-25   最后修改:2010-04-25
硬盘内的mp3文件太多了,下的时候没有归类,结果比较乱,就自己写了个小程序分拣.上代码:

...楼主的精神可嘉可是能用简单的方法就用最简单的方法解决

如果你使用windows, Win+F ---》输入 *.mp3-->回车 搜索出所有MP3文件。。。岂不是更简单?更省心?
0 请登录后投票
   发表时间:2010-04-26  
吃力不讨好的工作,很多mp3根本没有id3信息,这些只能是个玩具吧!!
0 请登录后投票
   发表时间:2010-04-26  
kjj 写道
吃力不讨好的工作,很多mp3根本没有id3信息,这些只能是个玩具吧!!

对,这个还真是个玩具,自己拿来分类本地歌曲的.
0 请登录后投票
   发表时间:2010-04-26  
xiaoyiz 写道
硬盘内的mp3文件太多了,下的时候没有归类,结果比较乱,就自己写了个小程序分拣.上代码:

...楼主的精神可嘉可是能用简单的方法就用最简单的方法解决

如果你使用windows, Win+F ---》输入 *.mp3-->回车 搜索出所有MP3文件。。。岂不是更简单?更省心?


这样能把各个文件放进歌手文件夹吗?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics