Remove all blank spaces and empty lines

后端 未结 7 1715
予麋鹿
予麋鹿 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 <String> DataArray = new ArrayList<String>();
        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<DataArray.size();i++)
        {
            bw.write(DataArray.get(i));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
    
    0 讨论(0)
提交回复
热议问题