Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Copy non-comment lines from one file to another: Files sometimes use a special initial character, like #, to indicate comments; this is common in Unix/Linux

Copy non-comment lines from one file to another:

Files sometimes use a special initial character, like #, to indicate comments; this is common in Unix/Linux shell scripts

Windows command files use the colon character, :

Write a program CopySkip, similar to CopyFile, that copies lines from an input file to an output file, but only those that do not begin with a specified character.

CopySkip takes three command line arguments, the input file name, the output file name, and the character, e.g.: java CopySkip in.txt out.txt "#"

Remember that args is a String array, so the character comes in as a String

Enclose the comment character in single or double quotes in the program call as shown above

Copy CopyFileMethod.java to CopySkip.java and change the copyFile method to be: public static void copySkip(String inFile, String outFile, char skip) Adjust the logic in this method so that lines starting with character skip are not written to the output file, and then call that method inmain to actually do the copying; be sure to pass in the first character of args[2] to be the parameter skip, that is, extract that character from the String args[2]

Your program will be tested by sending in sample files to copy that contain no comments to skip, some comments to skip, and all comments to skip; the test version of main will print the input file, copy the file using copySkip, and then print the output file to show that no comment lines were copied

If you want to test your program, the available input files for testing are nocomments.txt, somecomments.txt, and allcomments.txt - all of them use the '#' character to skip, and somecomments.txt also has lines that begin with the ':' character to skip, so you can try both

import java.io.*;

public class CopySkip { /* using IO methods, write a printFile method that prints the lines of a file */ public static void printFile(String fileName) { /* copy this method from CopyFileMethod here */ } /* using IO methods, write a copySkip method that copies one file to another but does not copy lines that begin with the third parameter's character skip; the output file must not exist, otherwise this method prints an error message */ public static void copySkip(String inFile, String outFile, char skip) { /* first copy the copyFile method body from your CopyFileMethod program here; as you process each line from the input file preparing to write it out, check if the input line is empty (.equals("")) - if so, just write it out, because it cannot begin with the skip character; if the input line is not empty, check to see if it's first character matches the skip character, and if so, do not write that line to the output file (skip it) */ } public static void main(String[] args) // uses program command line arguments { if (args.length != 2) { // change this to test for args.length != 3 System.out.println("Usage: CopyFile inputFile outputFile"); // change this to say CopySkip and add the skip character at the end of the printout System.exit(0); } /* Note: the available input files for testing are nocomments.txt, somecomments.txt, and allcomments.txt - all of them use the '#' character to skip, and somecomments.txt also has lines that begin with the ':' character to skip, so you can try both */ /* DO NOT MODIFY THE REST OF THIS MAIN METHOD, WHICH WILL BE USED FOR TESTING */ System.out.println("input file lines from " + args[0] + ":"); printFile(args[0]); System.out.println(); copySkip(args[0], args[1], args[2].charAt(0)); System.out.println("file lines after copying to " + args[1] + ":"); printFile(args[1]); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

Give an example of a low-level programming language.

Answered: 1 week ago

Question

gpt 1 1 9 .

Answered: 1 week ago