文件转写

来源:互联网 发布:python取小数点后两位 编辑:程序博客网 时间:2024/04/29 15:25
import java.io.*;       public class SrtTemplate {             public static void main(String[] args) {             File file=new File("D:/Downloads/videoes/srt/old.txt");     //源字幕文件位置             File file2=new File("D:/Downloads/videoes/srt/template.txt");     //新的字幕文件保存位置             int timeError=3;       //在此设定需要调整的时间秒数,正数为延迟,负数为提早     int id=1;           FileWriter fw=null;             try {                 file2.createNewFile();     //创建新文件                 fw = new FileWriter(file2);        //创建文件输出流             } catch (IOException ex) {                 ex.printStackTrace();             }              PrintWriter pw=new PrintWriter(fw); //包装文件输出流,方便整行写入             try {                 FileReader fr=new FileReader(file); //创建文件输入流                 BufferedReader in=new BufferedReader(fr);      //包装文件输入流,方便整行读取                               String line;                 StringBuffer newLine=new StringBuffer();                                             //以下while循环逐行读取字幕源文件                 while((line=in.readLine()) != null) {                         //使用静态方法进行正则式的匹配。  pw.println(id++);pw.println("00:00:00,000 --> 00:00:00,000");pw.println(line);pw.println();//如果读到的不是时间描述字符行,则原样写入新文件  //提早结束本次循环继续读取下一行                                       //以下对时间描述字符行进行格式转换和数学运算                                                     }                 pw.close();             } catch (FileNotFoundException ex) {                 ex.printStackTrace();             }catch (IOException ex) {                     ex.printStackTrace();             }         }        } 

0 0