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 a file, using a file dialog box.
(b) Make a copy of the file selected - whether it is a text file or an image file.
(c) Write to an output file with the option of either appending to a text file, or over-writing the contents of a text 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. Basi
(g) Tokenize the input file so that the program recognizes all printable characters on the keyboard.
NB:
- You must first select the file before items (b) (g) can be applicable
- You may utilize the classes GetData, BasicFile and TestBasicFile in the textbook as source of reference.
-
public class GetData {static double getDouble(String s)
-
{ return Double.parseDouble(getWord(s)); } static int getInt(String s) { return Integer.parseInt(getWord(s)); }
public static String getWord(String s) { return JOptionPane.showInputDialog(s); } }
-
import java.io.IOException; import javax.swing.JOptionPane;
/** * * @author oliva */ public class TestFile {
/** * @param args the command line arguments */ public static void main(String[] args) { boolean done = false; BasicFile f = new BasicFile(); String menu = "Enter option 1.collect an save data using PrintWriter 4.quit"; while (!done) { String s = JOptionPane.showInputDialog(menu); try { int i = Integer.parseInt(s); switch (i) { case 1: f.printWriter(); break; case 4: done = true; break; default: display("This option is underfined", "Error"); break; } } catch (NumberFormatException | NullPointerException | IOException e) { display(e.toString(), "Error"); } } }
static void display(String s, String err) { JOptionPane.showMessageDialog(null, s, err, JOptionPane.ERROR_MESSAGE); } }
-
public class BasicFile { File f; BasicFile(){ try{ JFileChooser choose = new JFileChooser("."); choose.setDialogTitle("Select or type file name to store database"); int status = choose.showSaveDialog(null); if(status != JFileChooser.APPROVE_OPTION)throw new IOException(); f = choose.getSelectedFile(); } catch(IOException e) { JOptionPane.showMessageDialog(null,e.toString(),"Sorry-change my mind", JOptionPane.WARNING_MESSAGE); System.exit(0); } } void printWriter() throws IOException{ NumberFormat nf = NumberFormat.getCurrencyInstance(); PrintWriter pw = new PrintWriter(f); pw.println("Last Name\tFirst Name\tAge\tSalary"); boolean done = false; while(!done){ int option = GetData.getInt("More data 1.yes 2.No"); switch(option) { case 1: //Reads the data String lname = GetData.getWord("Enter last name"); String fname = GetData.getWord("Enter first name"); double age = GetData.getDouble("Enter age by years"); double salary = GetData.getDouble("Enter salary"); //Write data to the file pw.print(lname+"\t"+fname+"\t\t"+age+"\t"+nf.format(salary)); pw.println(); break; case 2: done = true; break; default: break; } } pw.flush(); pw.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
