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

//Chapter 13 (File Input and Output), Java Programming, Joyce Faraell, 8th ed //

ID: 3681538 • Letter: #

Question

//Chapter 13 (File Input and Output), Java Programming, Joyce Faraell, 8th ed

//Output; need to fix on error.

//DebugThirteen1.java

// Program describes two files
// tells you which one is newer and which one is larger
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;
public class DebugThirteen1
{
   public static void main(String[] args)
   {
      Path file1 =
         Path.get("C:\Java\Chapter.13\DebugData1.txt");
      Path file2 =
         Path.get("C:\Java\Chapter.13\DebugData2.txt");
      try
      {
         BasicFileAttributes attr1 =
            Files.readAttributes(file1, BasicFileAttributes.class);
         System.out.println("File: " + file1.getFileName());
         System.out.println("Creation time " + attr1.CreationTime());
         System.out.println("Last modified time " + attr1.lastModifiedTime());
         System.out.println("Size " + attr1.sze());
         BasicFileAttributes attr2 =
            Files.readAttributes(file2, BasicFileAttributes.class);
         System.out.println(" File: " + file2.getFileName());
         System.out.println("Creation time " + attr2.creationTime());
         System.out.println("Last modified time " + attr2.lastModifiedTime());
         System.out.println("Size " + attr2.size());
         if(attr1.creationTime().compareTo(attr1.creationTime()) > 0)
            System.out.println(" " + file1getFileName() + " was created earlier");
         else
            System.out.println(" " + file1.getFileName() + " was not created earlier");
         if(attr1.size() > attr2.size())
            System.out.println(file1.getFileName() + " is larger ");
         else
            System.out.println(file3.getFileName() + " is not larger");
      }
      catch(IOException e)
      {
          System.out.println("IO Exception");
      }
   }
}
------------------------------------------------

//DebugData1.txt

Somewhere over the rainbow.

-----------------------------------------------

//DebugData2.txt

There's no place like home.
Are you a good witch or a bad witch?t

---------------------------------------------------

//DebugData3.txt

435-9845
239-9845
981-9883
384-5656
875-3784
874-8120
----------------------------------------------------

//DebugData4.txt

steak,20
lobster,30
macaroni,8
spaghetti,10

Explanation / Answer

Hi,

Here is the corrected answer.

package com.example.package; // Please use your own package
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;
public class DebugThirteen1
{
public static void main(String[] args)
{

try
{
   Path file1 =
       Paths.get("C:\Java\Chapter.13\\DebugData1.txt");
       Path file2 =
       Paths.get("C:\Java\Chapter.13\\DebugData2.txt");// Please use the path of the file from where you want to access it.
BasicFileAttributes attr1 =
Files.readAttributes(file1, BasicFileAttributes.class);
System.out.println("File: " + file1.getFileName());
System.out.println("Creation time " + attr1.creationTime());
System.out.println("Last modified time " + attr1.lastModifiedTime());
System.out.println("Size " + attr1.size());
BasicFileAttributes attr2 =
Files.readAttributes(file2, BasicFileAttributes.class);
System.out.println(" File: " + file2.getFileName());
System.out.println("Creation time " + attr2.creationTime());
System.out.println("Last modified time " + attr2.lastModifiedTime());
System.out.println("Size " + attr2.size());
if(attr1.creationTime().compareTo(attr1.creationTime()) > 0)
System.out.println(" " + file1.getFileName()+ " was created earlier");
else
System.out.println(" " + file1.getFileName() + " was not created earlier");
if(attr1.size() > attr2.size())
System.out.println(file1.getFileName() + " is larger ");
else
System.out.println(file2.getFileName() + " is not larger");
}
catch(IOException e)
{
System.out.println("IO Exception "+e.getMessage());
}
}
}

Output is as follow: Please make the changes in the program as per your requirements

File: DebugData1.txt
Creation time 2016-03-25T15:21:37.502029Z
Last modified time 2016-03-25T15:23:40.464062Z
Size 27

File: DebugData2.txt
Creation time 2016-03-25T15:23:18.874827Z
Last modified time 2016-03-25T15:23:55.322912Z
Size 66

DebugData1.txt was not created earlier
DebugData1.txt is not larger

Please revert back if you have any queries.

Thanks.