Creating a User access Control program in java that similuates UAC on windows XP
ID: 3764728 • Letter: C
Question
Creating a User access Control program in java that similuates UAC on windows XP, need help writing a login command in java that checks a user for a username and password
example of the "login" command
login username password
• Example: login name PasSworD
• Checks username and password against records in the accounts.txt file. If the username and
password are correct, logs the user into the system. Otherwise, displays and logs an error
message, then denies login.
• Things to consider / constraints:
o If a user is currently logged in, another user cannot login. (This system does not
support concurrent users.)
o Once logged in, the user will be able to use create, read, write, and
execute, and xcacls, (and net user and net group if an
Administrator) instructions which are allowed or denied according to the NTFS
file access rights security model.
o The user will also be able to use the logout as specified later in this document.
o There MUST be a user logged in to be able to use any command other than login.
Explanation / Answer
Answer:
Coreclass.java
import java.io.*;
import java.util.*;
public class Coreclass {
public static void main(String args[])
{
TextFileHelper TextFileH;
// where our text file will be created
File file =new File(" "); //Please Enter Your Own path
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
String fileName = "values.txt";
String actualFile =filePath+fileName;
try {
// initialize our TextFileHelper here
TextFileH = new TextFileHelper();
TextFileH.createPath(filePath);
// int ch=3;
int ch=0;
Scanner keyboard = new Scanner(System.in);
do{
System.out.println("Enter any option to 1.Create 2.Read 3.Update 4.Delete 5.Exit to Perform");
ch = keyboard.nextInt();
switch (ch)
{
case 1:
TextFileH.createTextFile(actualFile);
break;
case 2:
String contents = TextFileH.readTextFile(actualFile);
break;
case 3:
TextFileH.updateTextFile(actualFile, "Mike is so handsome!nNew line here!");
break;
case 4:
TextFileH.deleteTextFile(actualFile);
break;
case 5:
System.out.println("Program end.Thank You");
break;
default:
System.out.println("out");
break;
}
}while(ch!=5);
}
catch (NullPointerException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
TextFileHelper.java
import java.io.*;
public class TextFileHelper {
/*
* Create a text file.
*/
public void createTextFile(String actualFile) {
try {
File file = new File(actualFile);
if (file.exists()) {
flashMessage("Text file already exists.");
} else {
// create the text file
if (file.createNewFile()) {
flashMessage("Text file was created.");
} else {
flashMessage("Unable to create text file.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Read a text file.
*/
public String readTextFile(String actualFile) {
String contents = "";
try {
File file = new File(actualFile);
if (file.exists() && file.length() != 0) {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
contents += line + "n";
}
} else {
flashMessage("Unable to read. File may be missing or empty.");
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return contents;
}
/*
* Update a text file.
*/
public void updateTextFile(String actualFile, String contents) {
try {
File textFile = new File(actualFile);
if (textFile.exists()) {
FileWriter textFileWriter = new FileWriter(textFile, false);
BufferedWriter out = new BufferedWriter(textFileWriter);
String contentString = new String(contents);
// write the updated content
out.write(contentString);
out.close();
flashMessage("File was updated.");
} else {
flashMessage("Cannot update. File does not exist.");
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Delete a text file.
*/
public void deleteTextFile(String actualFile) {
try {
File file = new File(actualFile);
if (file.exists()) {
if (file.delete()) {
flashMessage("Text file was deleted!");
} else {
flashMessage("Unable to delete text file.");
}
} else {
flashMessage("Unable to delete. File does not exist.");
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createPath(String filePath) {
try {
File file = new File(filePath);
if (file.isDirectory()) {
flashMessage("Path exists.");
}
else {
if (file.mkdirs()) {
flashMessage("Path was created.");
} else {
flashMessage("Unable to create path.");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void flashMessage(String customText) {
try {
System.out.println(customText);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.