Question

Java!!! Write a program that takes a file name as its command line argument. It assumes...

Java!!!

Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

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

Answer:

Code:

I run some class files and exe files it satisfies for the class file

Output:

Raw_code:

import java.io.*; //importing io module for file methods
public class Bin_check {
   public static void main(String args[]){  
       try{
          String file = args[0];   //taking file name as command line argument
            FileInputStream fin=new FileInputStream(file);
            DataInputStream dis = new DataInputStream(fin); //getting data of a file
            int i = -889275714; // given value to check..
            if (i == dis.readInt()) //readInt() takes first 4 bytes of data
            {
               System.out.println("Yes"); //prints if it equal
            }
            else{
               System.out.println("No"); // prints if not equal
            }
          
            fin.close();  
       }
       //If file is not found Here printing exception
       catch(Exception e){System.out.println("There was an IO Exception Generated");}  
    }  
}

Thank You,

please do vote(Like)

Add a comment
Answer #2
pythonCopy codeimport sysdef check_integer_in_file(file_name):    try:        with open(file_name, "rb") as file:
            first_four_bytes = file.read(4)
            integer_value = int.from_bytes(first_four_bytes, byteorder='big', signed=True)            if integer_value == -889275714:                print("yes")            else:                print("no")    except FileNotFoundError:        print("Error: File not found.")    except IOError:        print("Error: IO Exception occurred.")if __name__ == "__main__":    if len(sys.argv) != 2:        print("Usage: python program_name.py file_name")    else:
        file_name = sys.argv[1]
        check_integer_in_file(file_name)

Trivia Answer: The value −889275714 is a specific signature called the "magic number" that appears at the beginning of compiled Java class files. The Java compiler inserts this signature to mark the file as a valid Java class file. By checking for this value, we can quickly identify whether a given file is a valid compiled Java class file or not. If the first four bytes contain this value, it is likely that the file is a Java class file. This technique is often used in various tools and utilities to identify and validate Java class files.


answered by: Mayre Yıldırım
Add a comment
Answer #3

In Java, you can use the following program to check the first 4 bytes of a binary data file and see if they contain the integer -889275714. It will output "yes" if the value is found, "no" if the value is not found, or an error message if there was an IOException generated while reading the file.

javaCopy codeimport java.io.DataInputStream;import java.io.FileInputStream;import java.io.IOException;public class BinaryFileChecker {    public static void main(String[] args) {        if (args.length != 1) {
            System.err.println("Usage: java BinaryFileChecker <file_name>");            return;
        }        String fileName = args[0];        try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(fileName))) {            int firstFourBytes = dataInputStream.readInt();            if (firstFourBytes == -889275714) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
        } catch (IOException e) {
            System.err.println("Error reading the file: " + e.getMessage());
        }
    }
}

Explanation:The program takes a file name as a command line argument and attempts to open the file as a binary data file using a DataInputStream. It then reads the first 4 bytes of the file using the readInt() method, which reads an integer (4 bytes) from the input stream.

The program checks if the integer value read from the file matches the value -889275714. If it does, it outputs "yes," indicating that the value is found. If the value does not match, it outputs "no."

The significance of testing for the value -889275714 lies in its use in Java class files. In Java bytecode, this value is known as the "magic number." It is a constant value that appears at the beginning of all valid Java class files. By checking for this specific value, the program can determine if the file follows the class file format used by Java.

answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
Java!!! Write a program that takes a file name as its command line argument. It assumes...
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
  • java) write a program that takes command line argument and compute the average of their maximum...

    java) write a program that takes command line argument and compute the average of their maximum and minimum. make sure there are command line arguments being passed before you attempt to compute anything

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • Write a program in C that takes a file name as the only argument on the...

    Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • I need help with a java program Write a program Enigma that takes a single String...

    I need help with a java program Write a program Enigma that takes a single String as a command line argument. Enigma should read the file specified by the String argument, add 5 to each byte, and leave the altered data values in a file whose name is the command line argument. Note that this "updating in place" is the most difficult part of this lab: java Enigma sophie.dat should read file sophie.dat, and upon completion, leave the modified data...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

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
ADVERTISEMENT