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 instructorExplanation / 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");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.