Question

Please answer this question using Java programming. (a) Write a method textFilter(String binaryName, String textName) of...

Please answer this question using Java programming.

(a) Write a method textFilter(String binaryName, String textName) of a class Filters which reads the binary byte file with name binaryName, removes all non-printable bytes (which are bytes with values < 32 or > 127) and outputs the remaining bytes to an ASCII text file with name textName. Handle possible exception(s) by outputting a suitable message using the getMessage() method if an exception occurs.


(b) What is the possible exception when the following statement is executed?

System.out.println("This is the first argument: " + args[0]);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

COMMENT IF YOU HAVE ANY DOUBTS AND LIKE THE ANSWER.

Answer for question a)

/**
* File Name : TextFilter.java
*
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;

public class TextFilter {

   /**
   * Method that takes two file names binaryName and textName as params
   * and then writes the printable characters into textName file as assci chars.
   *
   * @param binaryName
   * @param textName
   */
   public static void textFilter(String binaryName,String textName) {
       try {
           InputStream inp = new FileInputStream(binaryName);
           BufferedWriter bw = new BufferedWriter(new FileWriter(new File(textName)));
           int byteRead = inp.read();
           char byteToChar;
           while(byteRead != -1) {
               if(byteRead >=32 && byteRead <=127) {
                   byteToChar = (char)byteRead;
                   bw.write(byteToChar+"");
               }
               byteRead = inp.read();
           }
           bw.close();
           inp.close();
       }catch(Exception e) {
           System.out.println("Exception occured: "+e.getMessage());
       }
   }
   /**
   * An extra method added to write binary values -128 to 127 to passed file
   * as Byte data.
   * @param binaryName
   */
   public static void writeAllAsciiCharsToFile(String binaryName) {
       try {
           OutputStream out = new FileOutputStream(binaryName);
           for(Integer i = 1;i<=256;i++) {
               //values > 127 will automatically convert to negative values.
               //as it reaches the byte range by using byteValue() method.
               out.write(i.byteValue());
               System.out.println(i.byteValue());
           }
           out.close();
       }catch(Exception e) {
           System.out.println("Exception occured: "+e.getMessage());
       }
   }
   public static void main(String[] args) {
      
       String binaryName = "binaryData.txt";
       String textName="assciData.txt";
       textFilter(binaryName,textName);
//       writeAllAsciiCharsToFile("binaryData.txt");
      
      
   }
}


(b) What is the possible exception when the following statement is executed?

System.out.println("This is the first argument: " + args[0]);

ANSWER:

It throws ArrayOutOfBounds Exception, because if you didn't pass any arguments to the main method , then args array is empty and you are going to access 0_th index value in an empty array which results Array Index Out Of Bounds Exception.

Add a comment
Know the answer?
Add Answer to:
Please answer this question using Java programming. (a) Write a method textFilter(String binaryName, String textName) of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java programming to answer this question. Write a method countLines(String inFilename) of a class FileUtility...

    Using Java programming to answer this question. Write a method countLines(String inFilename) of a class FileUtility which reads a binary byte file with name inFilename using FileInputStream. The method returns the no. of lines containing comments starting with "//" with optional spaces (but not other characters) before it. Each line is terminated by '\n'. You may write other methods if necessary. Handle possible exception by outputting a suitable message if an exception occurs.

  • This is a java assignment Please help me Write the body of the fileAverage() method. Have...

    This is a java assignment Please help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Additionally, the file will have different contents during testing. public class Main { public double fileAverage( String filename ){    }...

  • Using listAllFiles as a guide, write a method that has one File argument: if the argument...

    Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f)   import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • [Java] Please test your code in the link I provide before you post your answer. The...

    [Java] Please test your code in the link I provide before you post your answer. The output should be looked like exact same as the tester. http://www.codecheck.it/files/17050415451csldwjahxt2kwn73ahd6vukt Thank you. Write a simplified application to illustrate the use of an undo button for a word processor. It keeps a history of all items and allows the user to undo the last. Write a class UndoStack. Implement using a Stack of Strings. The constructor will create an empty Stack to keep the...

  • Modification to be completed by group member 4: In the processLineOfData method, write the code to...

    Modification to be completed by group member 4: In the processLineOfData method, write the code to handle case "M" of the switch statement such that: A Manager object is created using the firstName, lastName, salary, and bonus local variables. Notice that salary and bonus need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(salary) Call the parsePaychecks method in this class passing the Manager object created in the previous step...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT