参照网上相关资料,实现向mp3文件后128字节写入指定数据,并读取出来.
项目中为向MP3文件写入歌曲信息,如歌手、专辑、歌名等.
第一步:创建关键类 SongInfo
public class SongInfo {private String TAG = "TAG"; // 文件头1-3
private String songName = "歌名"; // 歌曲名4-33
private String artist = "歌手名"; // 歌手名34-63
private String album = "专辑"; // 专辑名61-93
private String year = "2007"; // 年94-97
private String comment = "200"; // 备注98-125 java 放文件大小
private byte r1, r2, r3; // 三个保留位126,127,128
private boolean valid; // 是否合法
public transient String fileName; // 此歌曲对应的文件名,没有封装public SongInfo(byte[] data) {
if (data.length != 128) {
throw new RuntimeException("数据长度不合法:" + data.length);
}
String tag = "";
try {
tag = new String(data, 0, 3);
} catch (Exception e) {
System.out.println("error >> :"+e.getMessage());
e.printStackTrace();
}
// 只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("TAG")) {
try {
valid = true;
songName = new String(data, 3, 30,"UTF-8").trim();
artist = new String(data, 33, 30,"UTF-8").trim();
album = new String(data, 63, 30,"UTF-8").trim();
year = new String(data, 93, 4).trim();
comment = new String(data, 97, 28).trim();
r1 = data[125];
r2 = data[126];
r3 = data[127];
} catch (Exception e) {
System.out.println("error 22>> :"+e.getMessage());
e.printStackTrace();
}
} else {
valid = false;
}
}public SongInfo(String tag,String songName,String artist, String album,String fileSize) {
this.TAG = tag;
this.songName = songName;
this.artist = artist;
this.album = album;
this.comment = fileSize;
} public boolean isValid() {
return valid;
} public byte[] getBytes() {
byte[] data = new byte[128];
try {
System.arraycopy(TAG.getBytes(), 0, data, 0, 3);
byte[] temp;
temp = songName.getBytes("UTF-8");
System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length);
temp = artist.getBytes("UTF-8");
System.arraycopy(temp, 0, data, 33, temp.length > 30 ? 30: temp.length);
temp = album.getBytes("UTF-8");
System.arraycopy(temp, 0, data, 63, temp.length > 30 ? 30: temp.length);
temp = year.getBytes();
System.arraycopy(temp, 0, data, 93, temp.length > 4 ? 4 : temp.length);
temp = comment.getBytes();
System.arraycopy(temp, 0, data, 97, temp.length > 28 ? 28: temp.length); data[125] = r1;
data[126] = r2;
data[127] = r3;
} catch (Exception e) {
e.printStackTrace();
}
return data;
}public String getArtist() {
return artist;
}public void setArtist(String authorName) {
this.artist = authorName;
}public String getComment() {
return comment;
}public void setComment(String comment) {
this.comment = comment;
}public byte getR1() {
return r1;
}public void setR1(byte r1) {
this.r1 = r1;
}public byte getR2() {
return r2;
}public void setR2(byte r2) {
this.r2 = r2;
}public byte getR3() {
return r3;
}public void setR3(byte r3) {
this.r3 = r3;
}public String getSongName() {
return songName;
}public void setSongName(String songName) {
if (songName == null) {
throw new NullPointerException("歌名不能是null!");
}
valid = true;
this.songName = songName;
}public String getAlbum() {
return album;
}public void setAlbum(String specialName) {
this.album = specialName;
}public String getYear() {
return year;
}public void setYear(String year) {
this.year = year;
}
}
第二步: 调用此类实现写MP3文件后128字节数据操作
public void setSongInfo(String url, String tag, String songName,
String artist, String album, String fileSize) {
FileConnection fc = null;
SongInfo info = null;
String fileUrl = url;
OutputStream out = null;
byte[] buffer = new byte[128];
try {
fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE); if (fc.exists()) {
info = new SongInfo(tag, songName, artist, album, fileSize); buffer = info.getBytes();
out = fc.openOutputStream(fc.fileSize());
out.write(buffer);
out.flush();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
out.close();
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}第三步:读取后128字节内容
public SongInfo getSongInfo(String url) {
FileConnection fc = null;
SongInfo info = null;
InputStream in = null;
String songName = "song";
long songSize = 200;
String fileUrl = url;
byte[] buffer = new byte[128];
try {
fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);
if (fc.exists()) {
long size = fc.fileSize(); songName = fc.getName();
songName = songName.substring(0, songName.length() - 4);
songSize = fc.fileSize(); long byteOffset = size - 128;
in = fc.openInputStream();
in.skip(byteOffset);
in.read(buffer);
}
info = new SongInfo(buffer);
if (info.getSongName().equals("歌名")) {
info.setSongName(songName);
}
if (info.getComment().equals("200")) {
info.setComment("" + songSize);
}
if (info.getAlbum().equals("")) {
info.setAlbum("未知");
} } catch (IOException ioe) {
m_controll.setCurrent(new MessageAlert("warn",
" getSongInfo error: " + ioe.getMessage() + curURI, disp,
m_controll));
ioe.printStackTrace();
} finally {
try {
in.close();
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return info;
}
标签:String,temp,读写操作,mp3,128,byte,data,public,songName
From: https://blog.51cto.com/u_3124497/6913699