Java - File handling - String Manipulation - remove unnecessary space between words


// File Handling
// String manipulations

/*
Write a program that copies the content of one file to another by removing unnecessary space between words
*/

import java.io.*;

class Filespace
{
     public static void main(String as[]) throws IOException
     {
          FileReader fr = new FileReader("ipt2.txt");
          FileWriter fw = new FileWriter("opt2.txt");
          BufferedReader br = new BufferedReader(fr);
         
          while(true)
          {
              String str = br.readLine();
             
               if(str==null)
                   break;             
             
              String sa[] = str.split(" ");
             
              for(String s:sa)
              {  
                   s = s.trim();
                   if(s.length()!=0)
                        fw.write(s+" ");
              }
              fw.write("\n");
          }
          fr.close();
          fw.close();
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu