文件转写

该Java程序用于将字幕文件中的时间进行调整。它读取源字幕文件(old.txt),创建一个新的字幕文件(template.txt),并根据指定的时间误差(timeError)来提前或延迟每行字幕的时间。程序逐行读取源文件,识别时间描述行并进行相应的格式转换和数学运算,然后将处理后的字幕写入新文件。整个过程利用了FileReader、BufferedReader、FileWriter和PrintWriter等IO类进行文件操作。
摘要由CSDN通过智能技术生成
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();  
           }  
       }  
      
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值