⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gddjfilerenamemend.java

📁 音乐网站下载程序
💻 JAVA
字号:
package org.tools.mend;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.tools.UIHelper;

public class GDDJFileRenameMend extends JFrame {

	private JTextField d_dir_Field;
	private JTextField urlList_Field;
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			Font f = new Font("宋体", Font.PLAIN, 12);
				
			UIManager.put("Button.font",f);
	        UIManager.put("Label.font",f);
	        UIManager.put("List.font",f);
	        UIManager.put("omboBox.font",f); 
	        UIManager.put("Table.font",f);
	        UIManager.put("ToolTip.font",f);
	        UIManager.put("OptionPane.messageFont",f);   
	        UIManager.put("ToolTip.background",new Color(0xe7e7e7));
			GDDJFileRenameMend frame = new GDDJFileRenameMend();
			UIHelper.centerWithScreen(frame);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public GDDJFileRenameMend() {
		super();
		getContentPane().setLayout(null);
		setResizable(false);
		setTitle("广东DJ站文件下载重命名补丁 v1.0");
		setBounds(100, 100, 325, 115);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setIconImage(UIHelper.getImage("/res/icon.png"));

		final JLabel label = new JLabel();
		label.setText("URL列表文件");
		label.setBounds(5, 10, 76, 15);
		getContentPane().add(label);

		urlList_Field = new JTextField();
		urlList_Field.setEditable(false);
		urlList_Field.setBounds(80, 7, 128, 21);
		getContentPane().add(urlList_Field);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectUrlListFile();
			}
		});
		button.setMnemonic('f');
		button.setMnemonic(KeyEvent.VK_F);
		button.setText("选择(F)...");
		button.setBounds(214, 5, 95, 25);
		getContentPane().add(button);

		final JLabel label_1 = new JLabel();
		label_1.setText("已下载文件夹");
		label_1.setBounds(5, 38, 76, 15);
		getContentPane().add(label_1);

		d_dir_Field = new JTextField();
		d_dir_Field.setEditable(false);
		d_dir_Field.setBounds(80, 35, 128, 21);
		getContentPane().add(d_dir_Field);

		final JButton button_1 = new JButton();
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectDownedDirectory();
			}
		});
		button_1.setMnemonic(KeyEvent.VK_D);
		button_1.setText("选择(D)...");
		button_1.setBounds(214, 33, 95, 25);
		getContentPane().add(button_1);

		final JButton button_2 = new JButton();
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				doRename();
			}
		});
		button_2.setText("确定");
		button_2.setBounds(25, 60, 101, 25);
		button_2.setIcon(UIHelper.getImageIcon("/res/down.png"));
		getContentPane().add(button_2);

		final JButton button_2_1 = new JButton();
		button_2_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
		button_2_1.setText(" 取消");
		button_2_1.setBounds(194, 62, 101, 25);
		button_2_1.setIcon(UIHelper.getImageIcon("/res/exit.png"));
		getContentPane().add(button_2_1);
		//
	}

	private JFileChooser jfc = new JFileChooser();
	
	/**
	 * 
	 *
	 */
	private void close(){
		System.exit(0);
	}
	/**
	 * 
	 *
	 */
	private void selectUrlListFile(){
		urlList_Field.setText("");
		
		jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		if (jfc.showOpenDialog(this) == JFileChooser.CANCEL_OPTION)
			return;
		File file = jfc.getSelectedFile();
		if(file==null)return;
		
		urlList_Field.setText(file.getAbsolutePath());
	}
	/**
	 * 
	 *
	 */
	private void selectDownedDirectory(){
		d_dir_Field.setText("");
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if (jfc.showOpenDialog(this) == JFileChooser.CANCEL_OPTION)
			return;
		File file = jfc.getSelectedFile();
		if(file==null)return;
		d_dir_Field.setText(file.getAbsolutePath());
	}
	/**
	 * 
	 *
	 */
	private void doRename(){
		String d=d_dir_Field.getText();
		if(d.equals(""))return;
		Map <String,String>map = readList();
		if(map==null)return;
		File file = new File(d_dir_Field.getText());
		if(file.exists()&&file.isDirectory()){
			File f[] = file.listFiles();
			for(File _f:f){
				if(!_f.isFile())continue;
				else if(map.containsKey(_f.getName())){
					_f.renameTo(new File(d+File.separator+map.get(_f.getName())));
				}
			}
			JOptionPane.showMessageDialog(this, "已经成功重命名所有下载文件");
		}
		
	}
	
	private Map<String,String> readList(){
		if(urlList_Field.getText().equals(""))return null;
		File file = new File(urlList_Field.getText());

		String mes;
		try {
			BufferedReader fr = new BufferedReader(new FileReader(file));
			Map <String,String>map = new HashMap<String,String>();
			while((mes=fr.readLine())!=null){
				String m[] = mes.split("\\|");
				if(m.length!=2)continue;
				String o_name = m[1].substring(m[1].lastIndexOf("/")+1);
				String n_name = m[0].replaceAll("\\|", " ").replaceAll("/", " ").replaceAll(":", " ")+o_name.substring(o_name.lastIndexOf("."));
				map.put(o_name.replaceAll("\\r\\n", ""), n_name);
			}
			return map;
		} catch (IOException e) {
			return null;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -