Question: Write a Java program to imitate a file system of an operating system. In your solution, design a class called BasicFile with options to carry

Write a Java program to imitate a file system of an operating system.

In your solution, design a class called BasicFile with options to carry out the following operations:

(a) Select and open an input file using a file dialog box.

(b) Make a copy of the file, whether it is a text file or an image file.

(c) Write to an output file with the option of either appending to the file, or over-writing the contents of the file.

(d) Display the following attributes of the input file in a scrollable screen:

i. The absolute path of the file

ii. Files and directories that are in the path of the file.

iii. The size of the file in kilobytes.

iv. The number of lines in the file, if the is a text file.

(e) Display the contents of the input file in a scrollable pane.

(f) Search the input file line by line for a given string. The output must contain the line number, followed by the contents of the line that contains the search argument. For instance given the following the search string: Java, the program would search the file line by line generating a result such as the following:

50: on the island of Java

95: The people of JAVA loves jaVa.

(g) Tokenize the input file so that program recognizes all printable characters on the keyboard.

I just wanted to see if someone could help me make sure that this meets all the requirements listed above

Pastebin Link:

https://pastebin.com/CfL5dkfY

package systemfiles;

//import required packages

import java.io.FileNotFoundException;

import java.io.IOException;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

//Define the TestBasicFile

public class TestBasicFile

{

//Main method

public static void main(String[] args) throws FileNotFoundException, IOException {

boolean flag = false;

//menu options

String menu = "Java Menu 1. File size 2. Backup File "

+ " 3. File Content 4. Append a file 5. Write to a file "

+ "6. Search String in the file 7. Exit ";

while (!flag) {

//BasicFile object

BasicFile file = new BasicFile();

//Menu selection

String button = JOptionPane.showInputDialog(menu);

try {

//reading user menu choice

int a = Integer.parseInt(button);

//Different cases for users

switch (a) {

case 1://Information from file

JOptionPane.showMessageDialog(null,

"Select a file and the information will be shown",

"Select a file",

JOptionPane.INFORMATION_MESSAGE);

file.selectFile();

if (file.isValid()) {

display(file.toString(), "File");

} else {

file.selectFile();

}

break;

case 2:

//backup the file

file.selectFile();

if (file.isValid()) {

display(file.toString(), "File");

} else {

file.selectFile();

}

file.backupFile();

break;

case 3://selecting a file to display content

file.selectFile();

if (file.isValid()) {

display(file.wordCounting(), "File Content");

} else {

file.selectFile();

}

break;

case 4://appending a file

file.selectFile();

file.appendFile();

if (file.isValid()) {

display(file.toString(), "File");

} else {

file.appendFile();

}

break;

case 5://overwriting a file

file.selectFile();

file.printWriter();

if (file.isValid()) {

display(file.toString(), "File");

} else {

file.printWriter();

}

break;

case 6:

//display a file

file.selectFile();

file.fileReader();

//display a file

file.selectFile();

file.fileReader();

String input=GetData.getWord("Enter the word to search in the file");

//find the word java

file.findWord(input);

if (file.isValid())

{

display(file.toString(), "File");

}

else

{

file.fileReader();

}

break;

case 7:

//swtich exit

flag = true;

break;

default:

}//switch end

}//try end

catch (NumberFormatException | NullPointerException e) {

System.exit(0);

}

}//while end

}

//Display the message using scroll pane

public static void display(String button, String information)

{

JTextArea text = new JTextArea(button, 7, 20);

JScrollPane pane = new JScrollPane(text);

JOptionPane.showMessageDialog(null, pane, information, JOptionPane.INFORMATION_MESSAGE);

}

}

//BasicFile Class

package systemfiles;

//import required packages

import java.io.*;

import javax.swing.JOptionPane;

import javax.swing.JFileChooser;

import java.io.StreamTokenizer;

import java.text.NumberFormat;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.StringTokenizer;

//Define the class Basic File

public class BasicFile

{

//to create a file variable

File infile;

//To create a JFileChooser variable

JFileChooser select;

//Creating a file object

File f = new File(".", "File Backup");

//Basic File constructor

public BasicFile()

{

select = new JFileChooser(".");

}

//To check if file exists

boolean isValid()

{

return infile.exists();

}

//To select a file from local system locaton

public void selectFile()

{

int status = select.showOpenDialog(null);

try

{

if (status != JFileChooser.APPROVE_OPTION)

{

throw new IOException();

}

//selecting a file

infile = select.getSelectedFile();

if (!infile.exists())

{

throw new FileNotFoundException();

}

//catching exceptions

} catch (FileNotFoundException notFound)

{

JOptionPane.showMessageDialog(null, "No File Found ", "Error",

JOptionPane.INFORMATION_MESSAGE);

} catch (IOException notFound) {

System.exit(0);

}

}

//To implement the method to find the word in the file

public void findWord(String key) throws FileNotFoundException, IOException

{

try (BufferedReader br = new BufferedReader(new FileReader(infile)))

{

Recursion recObj=new Recursion();

int linecount=1;

StringTokenizer st ;

String line;

System.out.println(" ----------------- ");

while ((line = br.readLine()) != null)

{

st=new StringTokenizer(line);

if(recObj.findWord(st, key))

{

System.out.println(linecount+" : "+line);

}

linecount++;

}

}

}

//Read file

public void fileReader() throws IOException {

FileReader fileread = new FileReader(infile);

char arr[] = new char[(int) infile.length()];

fileread.read(arr);

for (char c : arr)

{

System.out.print(c);

fileread.close();

}

}

//Backup the file

public void backupFile() throws FileNotFoundException, IOException

{

DataInputStream in = null;

DataOutputStream out = null;

try {

in = new DataInputStream(new FileInputStream(infile));

out = new DataOutputStream(new FileOutputStream(f));

//writing to a new file

try {

while (true) {

byte a = in.readByte();

out.writeByte(a);

}

} catch (EOFException notFound) {

JOptionPane.showMessageDialog(null, "File was successfully backedup.",

"Success", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException notFound) {

JOptionPane.showMessageDialog(null, "Sorry, File not found",

"Error", JOptionPane.INFORMATION_MESSAGE);

}

} finally {

try {

in.close();

out.close();

} catch (IOException a) {

display(a.toString(), "Error");

}

}

}

//Add to a file

public void appendFile() throws IOException {

NumberFormat nf = NumberFormat.getCurrencyInstance();

FileWriter print = new FileWriter(infile);

boolean end = false;

while (!end)

{

int choice = GetData.getInt("Would you like to append a file? 1. Yes 2. No");

switch (choice)

{

case 1: // Read the data

String info = GetData.getWord("Please write what you would like to the file");

//Write data to file

print.append(" ");

print.append(info);

break;

case 2:

end = true;

break;

default:

break;

}//Switch end

}

print.flush();

print.close();

}

//Overwriting a file

public void printWriter() throws IOException {

NumberFormat nf = NumberFormat.getCurrencyInstance();

PrintWriter print = new PrintWriter(infile);

boolean end = false;

while (!end) {

int choice = GetData.getInt("Would you like to overwrite a file? 1. Yes 2. No");

switch (choice) {

case 1: // Read the data

String info = GetData.getWord("Please write what you would like to the file");

//Write data to file

print.print(info);

print.println();

break;

case 2:

end = true;

break;

default:

break;

}//switch end

}

print.flush();

print.close();

}

public String toString()

{

return infile.getName() + " " + infile.getAbsolutePath() + " "

+ infile.length() + " bytes";

}

//Line counter

public String wordCounting() {

try {

//variables

int counter = 0;

int numCount = 0;

int charCount = 0;

int lineCount = 1;

int totWords = 0;

FileReader a = new FileReader(infile);

StreamTokenizer b = new StreamTokenizer(a);

//Character counter

b.whitespaceChars(0, ' ');

b.resetSyntax();

b.wordChars('A', 'Z');

b.wordChars('a', 'z');

b.wordChars('0', '9');

b.wordChars(',', ',');

b.eolIsSignificant(true);

while (b.nextToken() != StreamTokenizer.TT_EOF) {

switch (b.ttype) {

case StreamTokenizer.TT_NUMBER:

numCount++;

break;

case StreamTokenizer.TT_WORD:

charCount += b.sval.length();

counter++;

break;

case StreamTokenizer.TT_EOL:

lineCount++;

break;

case StreamTokenizer.TT_EOF:

break;

default:

}

}

a.close();

totWords = numCount + counter;

return infile.getName() + " has " + lineCount + " line, "

+ totWords + " word, and "

+ charCount + " characters. ";

} catch (IOException c) {

display(c.toString(), "Error");

}

return " ";

}

//To display information in screen

public void display(String message, String x)

{

JOptionPane.showMessageDialog(null, message, x, JOptionPane.ERROR_MESSAGE);

}

}

//GetData Class

package systemfiles;

//This method read input in the output dialog box

import javax.swing.JOptionPane;

class GetData {

static double getDouble(String s) {

return Double.parseDouble(getWord(s));

}

static int getInt(String s) {

return Integer.parseInt(getWord(s));

}

static String getWord(String s) {

return JOptionPane.showInputDialog(s);

}

}

//Recursion Class

package systemfiles;

//This class to check whether the word in the file or not

import java.util.StringTokenizer;

//If exists return true,Otherwise false

class Recursion

{

boolean findWord(StringTokenizer t, String key)

{

if (!t.hasMoreTokens())

{

return false;

}

else if (t.nextToken().equalsIgnoreCase(key))

{

return true;

}

else

{

return findWord(t, key);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!