mp3文件后128字节歌曲信息读写操作

参照网上相关资料,实现向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;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用字节缓冲流实现二进制文件读写操作的Java代码,可以将wn.mp3文件复制为hl.mp3文件: ```java import java.io.*; public class BinaryFileCopy { public static void main(String[] args) { String sourceFileName = "wn.mp3"; // 原始文件名 String destinationFileName = "hl.mp3"; // 目标文件名 int bufferSize = 1024 * 1024; // 缓冲区大小 try (InputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFileName), bufferSize); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destinationFileName), bufferSize)) { byte[] buffer = new byte[bufferSize]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.flush(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码中,我们使用了字节缓冲输入流BufferedInputStream和字节缓冲输出流BufferedOutputStream,这两个流都是装饰器流,它们可以在内部维护一个缓冲区,可以提高读写效率。在使用这两个流时,我们需要提供一个缓冲区大小,这里我们设置为1MB。 在读写文件时,我们使用了一个byte数组作为缓存,每次从输入流中读取数据,然后写入到输出流中,直到读取完毕。最后,我们需要调用输出流的flush()方法,将缓存中的数据刷入文件中,确保文件的完整性。 注意:这里我们使用了Java 7中引入的try-with-resources语句,它可以自动关闭流,无需手动关闭。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值