Remove all blank spaces and empty lines

后端 未结 7 1719
予麋鹿
予麋鹿 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:09

    ...
    Scanner scanner = new Scanner(new File("infile.txt"));
    PrintStream out = new PrintStream(new File("outfile.txt"));
    while(scanner.hasNextLine()){
        String line = scanner.nextLine();
        line = line.trim();
        if(line.length() > 0)
            out.println(line);
    }
    ...
    

提交回复
热议问题