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

次のようなものを試すことができます:

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 に答える