View Full Version : Java tutorial problem

12-02-2011, 04:30 AM
Hey guys I'm new to this so bare with me.

Here is the problem, 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 file as FileStatistics.java

The book says the answer is this:

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.attribute.Attributes;
import java.io.IOException;
public class FileStatistics
public static void main(String[] args)
Path file =
int count = file.getNameCount();
System.out.println("Path is " + file.toString());
System.out.println("File name is " + file.getName());
System.out.println("Folder name is " + file.getName(count - 2));
BasicFileAttributes attr =
System.out.println("File's size is " + attr.size());
FileTime time = attr.creationTime();
System.out.println("File's creation time is " + time);
catch(IOException e)
System.out.println("IO Exception");

But this is the errors that I receive when I try to compile it: (I am using jGRASP)

----jGRASP exec: javac -g FileStatistics.java

FileStatistics.java:3: error: cannot find symbol
import java.nio.file.attribute.Attributes;
symbol: class Attributes
location: package java.nio.file.attribute
FileStatistics.java:15: error: method getName in interface Path cannot be applied to given types;
System.out.println("File name is " + file.getName());
required: int
found: no arguments
reason: actual and formal argument lists differ in length
FileStatistics.java:18: error: cannot find symbol
symbol: variable Attributes
location: class FileStatistics
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

I am not sure why I am getting the errors. Is there anything you can see that might be causing it?



12-02-2011, 06:06 PM
This is familiar, I think I answered a very similar recently too.
Here we go: http://www.codingforums.com/showthread.php?p=1145340
I can't be sure, but methinks Java 7 may not have been in a stable API at this point. I make specific note of a missing method within that CLASS, BUT! The Attributes class does not appear to exist within the java.nio.attributes package anymore. On top of this, Files no longer appears to have a method called .readBasicFileAttributes.
According to oracle, this was stable as of July 28, 2001. So I'm not sure whether the API is not correct, or if the methods are not correct. I don't develop in Java 7 at all, so I'm not sure which is best to trust.

I did find this in the API for BasicFileAttributes: BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);, which is similar to my previous post. So it looks to me like they dumped the readBasicFileAttributes and are now only supporting the readAttributes. I'm guessing they were slow updating their API from the last thread I replied to since Java 7 was stable at the time.

Check your book to see its published date. If it pre-dates July 28, 2011, then you will have intermittent available methods since the API was unstable at that point.