2

我有一个如下所示的 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);
        }
4

3 回答 3

1

你去吧。脚步:

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

于 2015-03-29T15:53:15.903 回答
1

您可以尝试以下方法:

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();
} 
于 2015-03-29T15:56:24.327 回答
1

希望这有帮助

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();
}
于 2015-03-29T15:59:21.297 回答