Question: Can someone help me with the following program. Note: BasicFile and TestFile are at the end. Write a Java program to imitate a file system

Can someone help me with the following program.

Note: BasicFile and TestFile are at the end.

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

Design a class called BasicFile with options to carry out the following operations:

1. Select and open an input file using a file dialog box.

2. Make a copy of the file, whether it is a text file or an image file.

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

4. 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.

5. Display the contents of the input file in a scrollable pane.

6. 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.

Use recursion to search the file.

7. Tokenize the input file so that program recognizes all printable characters on the keyboard.

You may utilize the classes BasicFile and TestBasicFile as a source of reference."

This is what i have so far

import java.io.*; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import java.io.StreamTokenizer; public class BasicFile { File f; JFileChooser select; File f2 = new File(".", "File Backup"); BasicFile() { select = new JFileChooser("."); } public void selectFile() { int status = select.showOpenDialog(null); try { if (status != JFileChooser.APPROVE_OPTION) throw new IOException(); f = select.getSelectedFile(); if (!f.exists()) throw new FileNotFoundException(); } catch(FileNotFoundException e) { JOptionPane.showMessageDialog(null, "No File Found ", "Error", JOptionPane.INFORMATION_MESSAGE); } catch(IOException e) { System.exit(0); } } void backupFile() throws FileNotFoundException { DataInputStream in = null; DataOutputStream out = null; try { in = new DataInputStream(new FileInputStream(f)); out = new DataOutputStream(new FileOutputStream(f2)); try { while (true) { byte data = in.readByte(); out.writeByte(data); } } catch(EOFException e) { JOptionPane.showMessageDialog(null, "File has been backedup.""Complete", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(null, "No File Found ","Error", JOptionPane.INFORMATION_MESSAGE); } } finally { try { in.close(); out.close(); } catch(Exception e) { display(e.toString(), "Error"); } } } boolean exists() { return f.exists(); } public String toString() { return f.getName() +" " +f.getAbsolutePath() +" " +f.length() +" bytes"; } public String wordCount() { try { int wordCount = 0, numberCount = 0, lineCount = 1, characterCount = 0, totalWords = 0; String c = " "; FileReader r = new FileReader(f); StreamTokenizer t = new StreamTokenizer(r); t.resetSyntax(); t.whitespaceChars(0, ' '); t.wordChars('a','z'); t.wordChars('A','Z'); t.wordChars('0','9'); t.eolIsSignificant(true); while(t.nextToken() != StreamTokenizer.TT_EOF) { switch(t.ttype) { case StreamTokenizer.TT_NUMBER: numberCount++; break; case StreamTokenizer.TT_WORD: characterCount += t.sval.length(); wordCount++; break; case StreamTokenizer.TT_EOL: lineCount++; break; case StreamTokenizer.TT_EOF: break; default: } } r.close(); totalWords = numberCount + wordCount; return f.getName() + " has " + lineCount + " line(s), " +totalWords + " word(s), and " + characterCount + " characters. "; } catch(IOException e) { display(e.toString(), "Error"); } return " "; } void display(String msg, String s) { JOptionPane.showMessageDialog(null,msg,s,JOptionPane.ERROR_MESSAGE); } }

Test file:

import java.io.FileNotFoundException; import java.io.IOException; import javax.swing.JOptionPane; public class TestBasicFile { public static void main(String[] args) throws FileNotFoundException, IOException { boolean done = false; String menu = "Menu 1. Specifications of File 2. Backup File 3. " +"Word Count 4. Exit"; while(!done) { BasicFile f = new BasicFile(); String s = JOptionPane.showInputDialog(menu); try { int i = Integer.parseInt(s); switch(i) { case 1: JOptionPane.showMessageDialog(null, "The name, path, and size of the file will be shown after it has been selected.","File Selection", JOptionPane.INFORMATION_MESSAGE); f.selectFile(); if(f.exists()) displayInfo(f.toString(), "File"); else f.selectFile(); break; case 2: f.selectFile(); if(f.exists()) displayInfo(f.toString(), "File"); else f.selectFile(); f.backupFile(); break; case 3: f.selectFile(); if(f.exists()) displayInfo(f.wordCount(), "Word Count"); else f.selectFile(); break; case 4: done = true; break; default: } } catch(NumberFormatException e) { System.exit(0); } catch(NullPointerException e) { System.exit(0); } } } static void displayInfo(String s, String info) { JOptionPane.showMessageDialog(null, s,info, JOptionPane.INFORMATION_MESSAGE); } }

however I dont think the program is complete,.It is not using recursion and is not showing anything on a scrollable pane can someone help me finish this and just check over to see I have exactly everything the program is asking for.

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!