字幕集合保存为srt文件

    技术2022-07-11  72

    字幕集合list保存为srt文件

    新建class,表示单个字幕数据的实体类 public class SrtEntity { /** * 字幕序号 */ public int number; /** * 开始时间 */ public String bg; /** * 结束时间 */ public String ed; /** * 字幕内容 */ public String content; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getBg() { return bg; } public void setBg(String bg) { this.bg = bg; } public String getEd() { return ed; } public void setEd(String ed) { this.ed = ed; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } 编写保存方法 /** * 编辑后的srtList保存为文件 * @param srtPath 文件保存路径 * @param srtList 字幕集合 */ public void editSrt(String srtPath, List<SrtEntity> srtList){ FileOutputStream writeFos = null; try { if (srtList != null && srtList.size() > 0){ writeFos = new FileOutputStream(srtPath); for (int i = 0; i < srtList.size(); i ++) { StringBuffer srtBuffer = new StringBuffer(""); srtBuffer.append(i + 1) .append("\r\n") .append(srtList.get(i).getBg()) .append(" --> ") .append(srtList.get(i).getEd()) .append("\r\n") .append(srtList.get(i).getContent()) .append("\r\n") .append("\r\n"); writeFos.write(srtBuffer.toString().getBytes()); } writeFos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { writeFos.close(); } catch (IOException e) { e.printStackTrace(); } }
    Processed: 0.015, SQL: 9