Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads text from one file and writes an edited version of th

ID: 3837481 • Letter: W

Question

Write a program that reads text from one file and writes an edited version of the same text to another file. The edited version is identical to the unedited version except that every string of two or more consecutive blanks is replaced by a single blank. Thus, the text is edited to remove any extra blank characters. Your program should define a function that is called with the input- and output-file streams as arguments. If this is being done as a class assignment, obtain the file names from your instructor

Explanation / Answer

//As the files name are not provided i have taken my own names.


import java.io.*;
import java.util.*;

public class FileIO {
public static void copyText(String inputFile, String outputFile)
{
  
try {
File input = new File((inputFile));
FileWriter writer = new FileWriter(outputFile, true);
Scanner sc = new Scanner(input);
while (sc.hasNextLine()) {
String s = sc.nextLine();
   String res="";
   int count=0;
   for(int i=0;i<s.length();i++)// Logic for remove extra space between strings
   {
       if(s.charAt(i)==' ')
       {
           count++;
       }
       else if(s.charAt(i)!=' ')
       {
           if(count>0)
           {
               res+=' ';
           }
           res+=s.charAt(i);
           count=0;
       }
          
   }
   System.out.println(res);
   writer.write(res);
   writer.write(" ");
}
writer.close();
}
catch(Exception exp)
{
   exp.printStackTrace();
}

}
public static void main(String args[]) {
   copyText("D:\input.txt","D:\output.txt");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote