Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP. Other CHEGG answers for this question do not address each issue of the project! Please help guys! Write a Java program to imitate

NEED HELP. Other CHEGG answers for this question do not address each issue of the project! Please help guys! 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 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.

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

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

here's what i have so far from other chegg answers, but i can't seem to figure out how this addresses everything the assignment asks for:

BASIC FILE

import java.io.*; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import java.io.StreamTokenizer;

public class BasicFile {

File f; JFileChooser fileSelect; File f2 = new File(".", "File Backup");

BasicFile() { fileSelect = new JFileChooser("."); }

public void fileSelecting() { int statusResult = fileSelect.showOpenDialog(null);

try { if (statusResult != JFileChooser.APPROVE_OPTION) { throw new IOException(); }

f = fileSelect.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 fileBackup() 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 backup completed.", "Complete", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(null, "File Not Found ", "Error", JOptionPane.INFORMATION_MESSAGE); } } finally { try { in.close(); out.close(); } catch (Exception e) { display(e.toString(), "Error"); } }

}

boolean exists() { return f.exists(); }

@Override 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;

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 CLASS

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. File Specifications 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.fileSelecting();

if (f.exists()) { displayInfo(f.toString(), "File"); } else { f.fileSelecting(); } break;

case 2: f.fileSelecting();

if (f.exists()) { displayInfo(f.toString(), "File"); } else { f.fileSelecting(); }

f.fileBackup(); break;

case 3: f.fileSelecting();

if (f.exists()) { displayInfo(f.wordCount(), "Word Count"); } else { f.fileSelecting(); } 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); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions