你去吧。脚步:
1)FileInputStream fin = new FileInputStream("line.srt");
这是在下一步中将文件获取到缓冲读取器
2)BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
将文本文件获取到缓冲区读取器
3)PrintWriter out = new PrintWriter("newline.srt");
使用打印编写器将每一行的字符串写入新文本文件中
4)String line = reader.readLine();
阅读下一行
5)while(line != null){
if (!line.trim().equals("")) {
检查该行不为空且该行不为空
6)out.println(line);
将行(非空)写入输出.srt文件
7)line = reader.readLine();
获取新行
8)out.close();
最后关闭 PrintWriter...
import java.io.*;
class RemoveBlankLine {
public static void main(String[] args) throws FileNotFoundException, IOException{
FileInputStream fin = new FileInputStream("line.srt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
PrintWriter out = new PrintWriter("newline.srt");
int i = 0;
String line = reader.readLine();
while(line != null){
if (!line.trim().equals("")) {
out.println(line);
}
line = reader.readLine();
}
out.close();
}
}
输入:
**
1
00:00:55,888 --> 00:00:57,875.
Antarctica
2
00:00:58,375 --> 00:01:01,512
An inhospitable wasteland.
**
输出:
**
1
00:00:55,888 --> 00:00:57,875.
Antarctica
2
00:00:58,375 --> 00:01:01,512
An inhospitable wasteland.
**
顺便说一句,请确保您在提问时清楚,因为您陈述问题的方式我假设 Line1、Line2 等是您的输入文件的一部分,并且我准备了另一个我必须更改的解决方案......确保您清楚准确,以便获得正确的答案!