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

help with a java program. Every Nth Word Purpose To enhance your understanding o

ID: 3864981 • Letter: H

Question

help with a java program.

Every Nth Word Purpose To enhance your understanding of file I/O Directions and example Write a program that prompts the user for a file name and an integer n. Print every nth word in the file to a separate file named name filename_copy.txt (filename being the file name the user gave you). When your program finishes, write the message File has been created. For this example, assume the file Test.txt contains: Hello my name is Joe and I am a farmer. Enter File Name: Test.txt Enter n: 2 File Test_copy.txt has been created. After your program runs as shown above, the file Test_copy.txt should contain: Hello name Joe I a

Explanation / Answer

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileSplit {
  

//Reading from a file
   public StringBuffer FileReader() throws IOException
   {
       FileReader fis = new FileReader("/FTest.txt");
       BufferedReader br = new BufferedReader(fis);
       String s =null;
       int n =2; //Value Entered by user runtime
      
       StringBuffer sb = new StringBuffer();
       try {
           if((s=br.readLine())!=null)
           {
              
               System.out.println("Hello from Splitter");
           /*System.out.println(s.length());*/
               String [] words = s.split("\s");
               for(int i =0 ; i<words.length;i=i+n)
               System.out.println(sb.append(words[i]));
               System.out.println(sb.toString());
              
           }
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
return sb;
      
   }
//Writing to a File   
   public void WriteToFile(StringBuffer s) throws Exception
   {
       FileWriter fo = new FileWriter("\Test_copy.txt");
       BufferedWriter br = new BufferedWriter(fo);
       br.write(s.toString());
       System.out.println("Content of String Buffer is into file ");
      
   }

}

import java.io.FileNotFoundException;
import java.io.IOException;

public class FileMain {

   public static void main(String[] args) throws Exception {
       // TODO Auto-generated method stub
      
       FileSplit fs = new FileSplit();
  
       StringBuffer s= fs.FileReader();
  
       fs.WriteToFile(s);

   }

}