Remove all blank spaces and empty lines

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

    Used to remove empty lines in same the file.

    public static void RemoveEmptyLines(String FilePath) throws IOException
    {
        File inputFile = new File(FilePath);
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        String inputFileReader;
        ArrayList  DataArray = new ArrayList();
        while((inputFileReader=reader.readLine())!=null)
        {
            if(inputFileReader.length()==0)
            {
                continue;
            }
            DataArray.add(inputFileReader);
        }
        reader.close();
    
        BufferedWriter bw = new BufferedWriter(new FileWriter(FilePath));
        for(int i=0;i

提交回复
热议问题