Question

In: Computer Science

Java Question here. Need to write a program that will check ANY operating system and find...

Java Question here. Need to write a program that will check ANY operating system and find out OS name and then write that info to a file in the data directory.

here is where I am so far:

    public void writeOSName() {
        
        // TODO write the name of the operating system running this code to a file
        //  in the data directory called os.txt
        
        // The file has to be written in the data directory of this project
        // Use System Properties to get the operating system name.
        // Remember this exact same code needs to work on a Mac/Linux and Windows computer without any modifications.
        
        // Handle any exceptions in this method. Do not declare that this method throws any exceptions.
        
        // If possible, test your code on two types of operating systems: Windows, and Mac/Linux.
        String osSystem = System.getProperty("os.name");
        if (osSystem.contains("Windows" )) {
           File filePath = new File("mydirectory", "os.txt");

        } else {

        }
        String version = System.getProperty("os.version");
//        System.out.println(osSystem);
//        System.out.println(version);
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("os.txt"))) {
            bw.write(System.getProperty("os.name", "\n"));
            //bw.write(System.getProperty("os.version", "\n"));

        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
    
}

Solutions

Expert Solution

Short Summary:

Implemented the code to find OS name and write to a file

Attached source code and output

**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

//This class finds the OS name and writes it to a file named OSName.txt
public class OSName {
   //Declare and assign osSystem variable
   static String osSystem = System.getProperty("os.name");

   //This method writes the OS name to the file
   public static void writeOSName() {

       /** TODO write the name of the operating system running this code to a file
in the data directory called os.txt

The file has to be written in the data directory of this project
Use System Properties to get the operating system name.
Remember this exact same code needs to work on a Mac/Linux and Windows computer without any modifications.

Handle any exceptions in this method. Do not declare that this method throws any exceptions.

If possible, test your code on two types of operating systems: Windows, and Mac/Linux. **/

       //File name and path, as required
       File file = new File("C:\\Users\\asus\\OSName.txt");

       try {
           //Initialize a buffered writer
           BufferedWriter bw = new BufferedWriter(new FileWriter(file));

           bw.write(osSystem);
           //Close writer
           bw.close();

       } catch (IOException e) {
           System.out.println("IO Error: " + e);
       }
       catch (Exception e) {
           System.out.println("Error: " + e);
       }


   }
   //Check if it's windows
   public static boolean isWindows() {

       return (osSystem.toLowerCase().indexOf("win") >= 0);

   }
   //Check if it's MAC

   public static boolean isMac() {

       return (osSystem.indexOf("mac") >= 0);

   }
   //Check if it's UNIX

   public static boolean isUnix() {

       return (osSystem.indexOf("nix") >= 0 || osSystem.indexOf("nux") >= 0 || osSystem.indexOf("aix") > 0 );

   }
   //Check if it's Solaris

   public static boolean isSolaris() {

       return (osSystem.indexOf("sunos") >= 0);

   }
   //Main method to test the above method
   public static void main(String args[])
   {
       //Write into the file based on the OS
       if(isWindows())
       {
           System.out.println("The windows OS name '"+osSystem+"' is written to the file OSName.txt");
           writeOSName();
       }
       if(isMac())
       {
           System.out.println("The MAC OS name '"+osSystem+"' is written to the file OSName.txt");
           writeOSName();
       }
       if(isUnix())
       {
           System.out.println("The UNIX OS name '"+osSystem+"' is written to the file OSName.txt");
           writeOSName();
       }
   }

}

Code Screenshot:

Output:

OSName.txt


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT