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

Write down the MINIMUM number of Java program code statements that are required

ID: 3572682 • Letter: W

Question

Write down the MINIMUM number of Java program code statements that are required to accomplish the following: Opening a file, which is named myDataFile.data, for reading data from the file (the opened file should be closed after reading from it. Opening a file, which is named myDataFile.data, for writing data to the file (the opened file should be closed after writing to it). Note the following information and instructions, for the foregoing questions 1a and 1b above: The statements that you write down should also include statements, which import any necessary packages or classes that are required. You DON'T have to write the definitions of a class or the main method you are required to write down ONLY the statements that are required to accomplish the goals mentioned in the foregoing points numbers 1a and 1b above. If you need to use an identifier, for any of the statements that you provide, create and use any valid Java identifier. For reading data from files and writing data to files, Java provides classes for reading and writing streams of bytes and classes for reading and writing streams of characters. Make sure that any Java classes you use are those classes for reading and writing streams of bytes, not those classes for reading and writing streams of characters. Use the following path, which is given below, as the path to the file i.e., the file which is named myDataFile.data to be opened, for either reading from the file or writing to the file C: My Documents My Java FilesmyDataFile.data

Explanation / Answer

a)

code to read the data from the file using byte streams.

code:

import java.io.FileInputStream;

import java.io.IOException;

FileInputStream fis=null;

try

{

       fis=new FileInputStream(“C:\My Documents\My Java Files\myDataFile.data”);

       int n;

       while((n=fis.read()) !=-1)

      {

      }

}

finally

   {

    if(fis!=null)

      {

fis.close();

      }

}

________________________

b)

code to write data to a file using byte streams

code:

import java.io.FileOutputStream;

import java.io.Exception;

FileOutputSteam fos=null;

try

(

  fos=new FiuleOutputStream(“C:\My Documents\My Java Files\myDataFile.data”);

  int n;

  out.wriite(n);

}

finally

{

if(fos != null)

{

   fos.close();

}

}

_______Thank You

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