如何在从文本文件中读取文本时删除空格(即以列的形式)

How to remove the white spaces while reading the text from Textfile (i.e In the forms of column)

我读取了其中数据以列的形式存储的文本文件,我想通过拆分所有行将数据存储在一个arrayList 中。 如何删除将数据存储到文本文件时出现的空格。

 try {
       fr = new FileReader(f);
       BufferedReader br = new BufferedReader(fr); 
       String skip=br.readLine();
       String line;
        String ss[]=null;
       while((line=br.readLine())!=null)
       {
        // ss=line.split(" ");
        // String nt=  line.replaceAll("\s","");
           StringTokenizer st = new StringTokenizer(line , " ");
        //   ss=line.split(" ");
         while(st.hasMoreTokens()){
         //  System.out.print(Integer.parseInt(ss[0].replaceAll("\s", "")));
      String   s1=st.nextToken();

     // ss[0]=s1;
             System.out.println(s1);
             break;
         }
       }

       EmployeeTaxReport emp=new EmployeeTaxReport(Integer.parseInt(ss[0].replaceAll("\s", "")), Integer.parseInt(ss[1].replaceAll("\s", "")),Integer.parseInt(ss[2].replaceAll("\s", "")));
       empreport.add(emp);
       for (EmployeeTaxReport e: empreport) {
         System.out.println(e.empid);

       }

   }catch(Exception ext)
   {
   //   System.out.println("OOPs..The file does not exit.Please input valid file name");
   ext.printStackTrace();
   }

如您的源代码所示,注释

//   ss=line.split(" ");

我相信你试过这个,发现它没有达到你想要的效果。

改为:

ss = line.split("\s+");

正如@ElliottFrisch 在评论中所建议的那样...