Remove all blank spaces and empty lines

后端 未结 7 1722
予麋鹿
予麋鹿 2020-12-11 17:02

How to remove all blank spaces and empty lines from a txt File using Java SE?

Input:

qwe
    qweqwe
  qwe



qwe

Output:

         


        
7条回答
  •  庸人自扰
    2020-12-11 17:20

    package com.home.interview;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class RemoveInReadFile {
    
        public static void main(String[] args) {
    
            try {
                Scanner scanner = new Scanner(new File("Readme.txt"));
    
    
                while(scanner.hasNext())
                {
                    String line = scanner.next();
    
                    String lineAfterTrim = line.trim();
                    System.out.print(lineAfterTrim);
                }
    
    
            } 
    
            catch (FileNotFoundException e) {
    
                e.printStackTrace();
            }
    
        }
    
    }
    

提交回复
热议问题