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

3.1 You are given a text file called “master” that is structured this way: the f

ID: 3831234 • Letter: 3

Question

3.1

You are given a text file called “master” that is structured this way: the first line contains a string which is the name of a file to be created. Subsequent lines are the names of other text files whose contents are to be copied into the new file, in the order that they appear. So, if master’s contents were:

bigfile

alist

blist

clist

then bigfile will contain the contents of alist, and tacked on to that will be the contents of blist followed by the contents of clist.

When finished, make sure that the data written to the new files have been flushed from their buffers and that any system resources used during the course of running your code have been released (Do not concern yourself with any possible exceptions here--assume they are handled elsewhere.)

Example Question and Answer

Q. Given a String variable named line1, write a sequence of statements that use a Scanner to read the first line of a file named "poem" and stores it in line1. (Do not concern yourself with any possible exceptions here--assume they are handled elsewhere.)

A. File fileInput = new File("poem"); Scanner scan = new Scanner(fileInput); line1=scan.nextLine();

Explanation / Answer

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class WriteToFile {
   public static void main(String[] args) {
       BufferedWriter writer;
       Scanner in;
       try {
           //Input file master.txt which contains files details
           File inputFile = new File("D:/workspace/chegg/src/master.txt");
           //Scanner object to read the input file
           in = new Scanner(inputFile);
          
           //Append txt at the end to create new file with name of the first line from master.txt
           String fileTosave = in.nextLine() + ".txt";
          
           // Create writer to write to file
           writer = new BufferedWriter(new FileWriter(fileTosave));
          
           //From second Line, read each line and assign the path of the files and the extensionas txt
           while (in.hasNextLine()) {
               String newFilePath = inputFile.getParentFile() + "/" + in.nextLine().trim() + ".txt";
               //Scanner object to read the contents of each file
               Scanner newFile = new Scanner(new File(newFilePath));
              
               //Store the content of files in the writer
               while (newFile.hasNextLine()) {
                   writer.write(newFile.nextLine());
                   writer.write(System.getProperty("line.separator"));
               }
               //close the scanner
               newFile.close();
           }
           //Flush the writer after writing contents into the file
           writer.flush();
           //Close the writer
           writer.close();
           //close the scanner
           in.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

}

Master.txt

bigfile
alist
blist
clist

alist.txt

Aitem1
Aitem2
Aline3
Aline4

blist.txt

Bitem1
Bitem2
Bline3
Bline4

clist.txt

Citem1
Citem2
Cline3
Cline4

Sample Ooutput:

Aitem1
Aitem2
Aline3
Aline4
Bitem1
Bitem2
Bline3
Bline4
Citem1
Citem2
Cline3
Cline4

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