Home > Java > javaTutorial > body text

Java example sharing of deleting blank lines from text files

高洛峰
Release: 2017-01-11 15:09:55
Original
1530 people have browsed it

javaDelete empty lines in text files

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class qukonghang {
 private static String filename1;
 private static String filename2;
 public static void main(String[] args) {
  filename1="file1";
  filename2="file2";
  File file=new File(filename1);
  InputStream is=null;
  BufferedReader br = null;
  String tmp;
  FileWriter writer=null;
  int i=0;
  try {
   is=new BufferedInputStream(new FileInputStream(file));
   br = new BufferedReader(new InputStreamReader(is, "utf-8"));
   writer = new FileWriter(filename2, true);
   while((tmp=br.readLine())!=null){
    if(tmp.equals(""));
    else{
     writer.write(tmp+"\n");
     i++;
     System.out.println(i);
    }
   }
   writer.close();
   is.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
Copy after login

For more examples of Java implementation of deleting empty lines in text files, please pay attention to the PHP Chinese website for related articles!

For more examples of Java implementation of deleting blank lines in text files, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template