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

📄 datamanager.java

📁 自己用Java写的一个mp3文件改名的小工具
💻 JAVA
字号:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Observable;
import java.util.Vector;

/**
 *  数据处理类
 *
 * @author     kilo
 * @created    2003年12月22日
 */
public final class DataManager
	extends Observable {
	/**
	 *  这里使用了单例模式。
	 */
	private static DataManager theManager;
	/**
	 *  原文件名
	 */
	public String curName;
	/**
	 *  新文件名或者改名失败的出错信息
	 */
	public String newName;
	/**
	 *  是否为新的改名操作,如果是新的,则更新用户界面
	 */
	public boolean isNewOp;
	/**
	 *  需要改名的mp3文件组
	 */
	private Vector fileArray;
	/**
	 *  是否成功改名 true/false
	 */
	public boolean isOK;
	/**
	 *  当前需要改名的mp3文件的ID3标签类
	 */
	private ID3Interface theID3;
	/**
	 *  ID3标签工厂
	 */
	private ID3VxConFactory theFactory = new ID3VxConFactory();


	/**
	 *  Constructor for the DataManager object
	 */
	private DataManager() {
		fileArray = new Vector();
	}


	/**
	 *  得到DataManager类的实例。DataManager类使用了单例模式。
	 *
	 * @return    DataManager类的实例
	 */
	public static DataManager getInstance() {
		if ( theManager == null )
			theManager = new DataManager();

		return theManager;
	}


	/**
	 *  更新用户界面,调用getFileArray()过滤当前用户选择的所有文件,将过滤得到的mp3文件加到向量fileArray中
	 *
	 * @param  f  用户选择的所有文件和目录
	 */
	public void setSelectedFiles( File[] f ) {
		fileArray.clear();
		isNewOp = true;
		setChanged();
		notifyObservers();
		isNewOp = false;
		getFileArray( f );
	}


	/**
	 *  过滤当前用户选择的所有文件,将过滤得到的mp3文件加到向量fileArray中
	 *
	 * @param  f  用户选择的所有文件和目录
	 */
	private void getFileArray( File[] f ) {
		int i = 0;
		while ( i < f.length ) {
			if ( !f[i].exists() ) {
				i++;
				continue;
			}
			if ( f[i].isDirectory() ) {
				File[] tmpfile = f[i].listFiles( new m_ioFileFilter( true, "mp3" ) );
				if ( tmpfile.length != 0 )
					getFileArray( tmpfile );

				else {
					curName = f[i].getAbsolutePath();
					newName = "此目录下没有找到mp3文件";
					isOK = false;
					setChanged();
					notifyObservers();
				}
			}
			else
				fileArray.addElement( f[i] );

			i++;
		}

	}


	/**
	 *  循环对每一个mp3文件进行改名操作直到所用的文件都已经改名,并且通知界面类改名结果
	 */
	public void startRename() {
		File tmp;
		curName = "";
		newName = "";
		String[] nameInclude = {
		                               "Artist", "Title"};

		for ( int i = 0; i < fileArray.size(); i++ ) {
			tmp = (File)( fileArray.get( i ) );
			isOK = false;
			curName = tmp.getPath();
			try {
				if ( setFileName( tmp ) ) {
					isOK = setNewName( nameInclude );
					if ( !isOK ) {
						newName = "文件被其他程序占用,不能改名";
					}
				}
				else {
					newName = "文件中未找到歌曲信息,原文件未改名";

				}
			}
			catch ( FileNotFoundException e ) {
				newName = "文件未找到";
			}
			catch ( BadID3Exception e ) {
				newName = "损坏的歌曲信息,改名失败";
			}
			catch ( FileExistException e ) {
				newName = "文件 " + newName + " 已经存在,原文件未改名";
			}
			catch ( SecurityException e ) {
				newName = "权限不够,改名失败";

			}
			catch ( IOException e ) {
				newName = "磁盘操作发生错误,改名失败";

			}
			setChanged();
			notifyObservers();
		}
	}


	/**
	 *  Sets the newName attribute of the DataManager object
	 *
	 * @param  frameID                   新文件名中应当包含的歌曲信息
         * @return                          改名是否成功 true/false
	 * @exception  IOException
	 * @exception  BadID3Exception      损坏的ID3标签异常
	 * @exception  FileNotFoundException
	 * @exception  SecurityException
	 * @exception  FileExistException
	 */
	private boolean setNewName( String[] frameID )
	throws IOException,
				BadID3Exception, FileNotFoundException, SecurityException,
		FileExistException, IOException {

		if ( theID3 != null ) {
			newName = theID3.getNewName( frameID, false );
			return theID3.setNewName( frameID, newName );
		}
		return false;
	}


	/**
	 *  Sets the fileName attribute of the DataManager object
	 *
	 * @param  filename                 需要改名的mp3文件,可以是一个file类,也可以是文件名
	 * @return                          相应文件的ID3Interface类
	 * @exception  FileNotFoundExceptionn
	 * @exception  IOException
	 * @exception  BadID3Exception     损坏的ID3标签异
	 */
	private boolean setFileName( Object filename )
	throws FileNotFoundException,
		IOException, BadID3Exception {
		theID3 = theFactory.getID3Controler( filename );
		return ( theID3 != null );
	}

}

⌨️ 快捷键说明

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