gpt4 book ai didi

java - 如何使用 Java 忽略 srt 文件中的空行

转载 作者:行者123 更新时间:2023-11-30 08:14:41 24 4
gpt4 key购买 nike

我有一个如下所示的 srt 文件,我想删除空行:第 3 行

**
1
Line1: 00:00:55,888 --> 00:00:57,875.
Line2:Antarctica
Line3:
Line4:2
Line5:00:00:58,375 --> 00:01:01,512
Line6:An inhospitable wasteland.
**
        FileInputStream fin = new FileInputStream("line.srt");
FileOutputStream fout = new FileOutputStream("m/line.srt");
int i = 0;
while(((i =fin.read()) != -1)){
if(i != 0)
fout.write((byte)i);
}

最佳答案

给你。步骤:

1) FileInputStream fin = new FileInputStream("line.srt");这是为了下一步把文件拿到bufferedreader

2) BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); 获取文本文件到 buffereder

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 等是你的输入文件的一部分,我已经准备了另一个我必须改变的解决方案... 确保您清晰准确,以便获得正确的答案!

关于java - 如何使用 Java 忽略 srt 文件中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29330983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com