In: Computer Science
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); } } }
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!******************