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

How can I ignore a blank line in a srt file using Java

我有一个如下所示的 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");这是为了在下一步中将文件获取到缓冲读取器

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

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

您可以尝试类似的方法:

BufferedReader br = null;
BufferedWriter bw = null;

try {
    br = new BufferedReader(new FileReader("line.srt"));
    bw = new BufferedWriter(new FileWriter("m/line.srt"));

    for(String line; (line = br.readLine()) != null; ) {
        if(line.trim().length() == 0) {
            continue;
        } else {
            bw.write(line);
            bw.newLine();
        }
    }

    bw.flush();
    bw.close();
    br.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} 

希望对您有所帮助

public static void main(String[] args) throws FileNotFoundException, IOException {
  Path myPath = Paths.get("e:\", "1.txt");
  List<String> ls ;
  ls = Files.readAllLines(myPath, StandardCharsets.US_ASCII);
  PrintWriter out = new PrintWriter("e:\2.txt");

    for (int i = 0; i < ls.size(); i++) {
        String []temp = ls.get(i).split(":");
        if(temp.length>1) {
           out.println(ls.get(i));
        }
    }
    out.close();
}