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

This program will display results of a file. Please create a directory on your s

ID: 640582 • Letter: T

Question

This program will display results of a file.

Please create a directory on your system and use the file name: "C:StLeoCOM209Chp13TestFile.txt"

When I run your program this should be the file you are using. I will have the file setup on my system so I don't have to change the filename in everyone's code.

Please display:

FileName:

FileSize:

Created:

Number of Elements:

Element 0:

Element 1: etc.

Output should look like this:

Filename:TestFile.txt

FileSize: 7 (this will vary on your system, depending on what you put in the file)

Created: 2015-02-03T20:19:04.680399Z (this too will vary, depending on when you create it)

Number of Elements:4

Element 0 = StLeo

Element 1 = COM209

Element 2 = Chp13

Element 3 = TestFile.txt

Explanation / Answer

//Java program that displays the file name,size, date of cration and elements

//in the file path

//FileProgram.java
import java.io.File;
import java.sql.Date;
import java.util.StringTokenizer;
public class FileProgram
{
   public static void main(String[] args)
   {
      
       //Create a file path
       String filePath="C:\StLeo\COM209\Chp13\TestFile.txt";
       //Create a File class
       File file=new File(filePath);
      
       //print name of the file
       System.out.println("File Name :"+file.getName());
       //print lenght of the file
       System.out.println("File Size:"+file.length());
       //Get the time of creation
       long milliseconds=file.lastModified();
       //Convert the last milliseconds to Date class
       Date date=new Date(milliseconds);
       //Print date object
       System.out.println("Created :"+date);
      
       //user stringtokenizer class to split the path into elements
       StringTokenizer tokens=new StringTokenizer(filePath,"\");
       int count=0;
      
       //use hasMoreElements to check if the elements are exists
       while(tokens.hasMoreElements())
       {
           //print the elements in path
           System.out.println("Element "+count+": "+tokens.nextElement());
           count++;
       }
      
      
      
      
   }
}//end of the class


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

Sample Output:

File Name :TestFile.txt
File Size:20
Created :2015-02-13
Element 0: C:
Element 1: StLeo
Element 2: COM209
Element 3: Chp13
Element 4: TestFile.txt

Hope this helps 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