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

1. Create a file using any word-processing program or text editor. Write an appl

ID: 3848384 • Letter: 1

Question

1. Create a file using any word-processing program or text editor. Write an application that displays the file’s name, containing folder, size, and time of last modification. Save the file as FileStatistics.java.

Below is a shell I have

1 import java.nio.file.*;
2 import java.nio.file.attribute.*;
3 import java.io.IOException;
4 public class FileStatistics
5 {
6 public static void main(String[] args)
7 {
8 Path file =
9 Paths.get("C:\Java\Chapter.13\TestData.txt");
10 try
11 {
12 // declare count and then display path, file name and folder name
13
14
15
16 // declare a BasicFileAttributes object, then add statements to display the file's size and creation time
17
18 }
19
20 catch(IOException e)
21 {
22 // add display IOException
23
24 }
25 }

Explanation / Answer

Below is your code: -


import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException;

public class FileStatistics {
   public static void main(String[] args) {
       Path file = Paths.get("C:\Java\Chapter.13\TestData.txt");
       try {
           // declare count and then display path, file name and folder name
           System.out.println("Path: "+file.toString());
           System.out.println("File Name: "+file.getFileName());
           System.out.println("Folder Name: "+file.getParent().getFileName());
           // declare a BasicFileAttributes object, then add statements to
           // display the file's size and creation time
           BasicFileAttributes obj = Files.readAttributes(file, BasicFileAttributes.class);
           System.out.println("File Size: "+obj.size()+"bytes");
           System.out.println("File creation Time: "+obj.creationTime());
           System.out.println("File Modification time: "+obj.lastModifiedTime());

       } catch (IOException e) {
           // add display IOException
           System.out.println("There is some problem accessing the file. Please contact administrator.");

       }
   }
}

Sample Run : -

1. When file is not there

Path: C:JavaChapter.13TestData.txt
File Name: TestData.txt
Folder Name: Chapter.13
There is some problem accessing the file. Please contact administrator.

2.

//When file is there: -

Path: C:JavaChapter.13TestData.txt
File Name: TestData.txt
Folder Name: Chapter.13
File Size: 96bytes
File creation Time: 2017-06-12T03:41:40.656126Z
File Modification time: 2017-06-12T03:44:22.667325Z