📄 id3interface.java
字号:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Description of the Class
*
* @author kilo
* @created 2003年12月22日
*/
public abstract class ID3Interface {
/**
* 当前文件的RandomAccessFile类
*/
protected RandomAccessFile cAFile;
/**
* 当前文件的File类
*/
protected File cFile;
/**
* 检查ID3标签
*
* @param fileName 需要检测的文件的文件名
* @return 是否有ID3标签 true/falsee
* @exception IOException n
* @exception BadID3Exception 损坏的ID3标签异常
*/
public static boolean ID3Check( Object fileName )
throws IOException , BadID3Exception {
return false;
}
/**
* 得到ID3标签中某一个标签帧的内容
*
* @param frameID 需要得到的标签帧的名字
* @return 标签帧的内容
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
*/
public final String getfrmContext( String frameID )
throws IOException , BadID3Exception {
return getFrame( encodeFrame( frameID ) );
}
/**
* 更改ID3标签中某一个标签帧的内容
*
* @param frameID 需要更改的标签帧的名字
* @param frmContext 需要更改的标签帧的名字内容
* @return 是否更改成功 true/false
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
*/
public final boolean setfrmContext( String frameID , String frmContext )
throws IOException , BadID3Exception {
return setFrame( encodeFrame( frameID ) , frmContext );
}
/**
* 进行改名操作
*
* @param frameID 新文件名中应当包含的歌曲信息
* @param newName mp3文件的新名字
* @return 是否改名成功 true/false
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
* @exception FileNotFoundException
* @exception SecurityException
* @exception FileExistException 新文件已经存在异常
*/
public final boolean setNewName( String[] frameID , String newName )
throws IOException , BadID3Exception , FileNotFoundException ,
SecurityException , FileExistException {
File newFile;
String absNewName;
try {
absNewName = getNewName( frameID , true );
}
finally {
cAFile.close();
}
if ( absNewName.equals( cFile.getPath() ) ) {
return true;
}
newFile = new File( absNewName );
if ( newFile.exists() ) {
throw new FileExistException();
}
if ( cFile.renameTo( newFile ) ) {
return true;
}
return false;
}
/**
* 操作系统中文件名不能包含一些特殊字符,这里将其过滤掉
*
* @param newN 未处理的新文件名
* @return 经过过滤后的新文件名e
*/
private final String filter( String newN ) {
newN = newN.replace( File.separatorChar , '_' );
newN = newN.replace( File.pathSeparatorChar , ' ' );
newN = newN.replace( '\\' , ' ' );
newN = newN.replace( '/' , ' ' );
newN = newN.replace( '?' , ' ' );
newN = newN.replace( ':' , ' ' );
newN = newN.replace( '<' , ' ' );
newN = newN.replace( '>' , ' ' );
newN = newN.replace( '*' , ' ' );
newN = newN.replace( '|' , ' ' );
return newN;
}
/**
* 得到新文件名
*
* @param frameID 新文件名中应当包含的歌曲信息
* @param isAbsName 是否需要绝对文件名(包含绝对路径的文件名)
* @return 新文件名
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
* @exception FileNotFoundException
* @exception SecurityException
*/
public final String getNewName( String[] frameID , boolean isAbsName )
throws IOException , BadID3Exception , FileNotFoundException ,
SecurityException {
if ( cFile != null ) {
String singer;
String musicName;
String newName;
String absNewName;
singer = getfrmContext( frameID[0] ).trim();
musicName = getfrmContext( frameID[1] ).trim();
if ( "".equals( musicName ) ) {
throw new BadID3Exception();
}
else if ( !"".equals( singer ) && !"".equals( musicName ) ) {
newName = singer + "_" + musicName;
}
else {
newName = musicName;
}
if ( !"".equals( newName ) ) {
newName += ".mp3";
}
else {
throw new BadID3Exception();
}
newName = filter( newName );
if ( cFile.getParent().lastIndexOf( File.separator )
== cFile.getParent().length() - 1 ) {
absNewName = cFile.getParent() + newName;
}
else {
absNewName = cFile.getParent() + File.separator + newName;
}
return isAbsName ? absNewName : newName;
}
else {
throw new FileNotFoundException();
}
}
/**
* 设置需要改名的mp3文件
*
* @param filename 需要改名的mp3文件
* @exception BadID3Exception 损坏的ID3标签异常
* @exception FileNotFoundException
* @exception IOException
*/
public final void setFileName( Object filename )
throws BadID3Exception ,
FileNotFoundException , IOException {
cAFile = null;
if ( filename instanceof String ) {
cFile = new File( ( String ) filename );
cAFile = new RandomAccessFile( ( String ) filename , "r" );
}
else if ( filename instanceof File ) {
cFile = ( File ) filename;
cAFile = new RandomAccessFile( ( File ) filename , "r" );
}
if ( cAFile == null ) {
throw new BadID3Exception();
}
initFiles( filename );
}
/**
* 得到某一个标签帧的内容
*
* @param frameID 需要得到的标签帧名
* @return 需要得到的标签帧内容
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
*/
protected abstract String getFrame( String frameID )
throws IOException , BadID3Exception;
/**
* 设置某一个标签帧的内容
*
* @param frameID 需要设置的标签帧名
* @param frmContext 需要设置的标签帧内容
* @return 是否更改内容成功 true/false
* @exception IOException
* @exception BadID3Exception 损坏的ID3标签异常
*/
protected abstract boolean setFrame( String frameID , String frmContext )
throws IOException , BadID3Exception;
/**
* 将不同的ID3标签帧名统一
*
* @param frameID ID3标签标签帧名
* @return 统一后的ID3标签标签帧名
*/
protected abstract String encodeFrame( String frameID );
/**
* 初始化类,设置cFile、cAFile
*
* @param filename mp3文件名
* @exception BadID3Exception 损坏的ID3标签异常
* @exception FileNotFoundException
* @exception IOException
*/
protected abstract void initFiles( Object filename )
throws BadID3Exception , FileNotFoundException , IOException;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -