Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File Operations - Write a Java program to imitate a certain file system and cert

ID: 3761317 • Letter: F

Question

File Operations - Write a Java program to imitate a certain file system and certain utility programs of an operating system. • Select a file from a file dialog box, using the class JFileChooser. • If the file is readable (text file), then process the file as follow, otherwise display a message stating that the file is not readable. Process the readable file: • Buffer chunks of 32 bytes; each row saves id (in column 1, succession number starting from 1) and the 32 bytes. The buffer size of the last row may be less than 32 byes, in which case pad it with ‘*’. • Display the content of the buffer. • Save the buffer in a file, where the file name is buffOut.txt

Explanation / Answer

package TestBasicFile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class BasicFile
{
File f;
JFileChooser select;
File f2 = new File(".", "buffin.txt");

BasicFile()
{
   this.select = new JFileChooser(".");
}

public void selectFile()
{
   int status = this.select.showOpenDialog(null);
   try
   {
      if (status != 0) throw new IOException();
      this.f = this.select.getSelectedFile();

      if (!(this.f.exists())) throw new FileNotFoundException();
   }
   catch (FileNotFoundException e)
   {
      JOptionPane.showMessageDialog(null, "File Not Found ", "Error", 1);
   }
   catch (IOException e)
   {
      System.exit(0);
   }
}

void backupFile() throws FileNotFoundException
{
   DataInputStream in = null;
   DataOutputStream out = null;
   try
   {
      in = new DataInputStream(new FileInputStream(this.f));
      out = new DataOutputStream(new FileOutputStream(this.f2));
      label78:
      while (true)
      {
       try
       {
          byte data = in.readByte();
          out.writeByte(data);
       }
       catch (EOFException e)
       {
          JOptionPane.showMessageDialog(null, "Success!!!", "Backup Complete!", 1);

          break label78:
       }
       catch (IOException e)
       {
          JOptionPane.showMessageDialog(null, "File Not Found ", "Error", 1);
       }
      }
   }
   finally
   {
      try
      {
       in.close();
       out.close();
      }
      catch (Exception e)
      {
       display(e.toString(), "Error");
      }
   }
}

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

public String toString()
{
   return this.f.getName() + " " + this.f.getAbsolutePath() + " " + this.f.length() + " bytes";
}

void display(String msg, String s)
{
   JOptionPane.showMessageDialog(null, msg, s, 0);
}
}

package TestBasicFile;

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 = "Enter option 1. File Description 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, "When file is selected, it will display name, path, and size", "File Selection", 1);

          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:
          done = true;
       }

      }
      catch (NumberFormatException e)
      {
       System.exit(0);
      }
      catch (NullPointerException e)
      {
       System.exit(0);
      }
   }
}

static void displayInfo(String s, String info)
{
   JOptionPane.showMessageDialog(null, s, info, 1);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote