Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java tutorial problem

    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 =
    Paths.get("C:\\Java\\Chapter.13\\TestData.txt");
    try
    {
    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 =
    Attributes.readBasicFileAttributes(file);
    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
    Attributes.readBasicFileAttributes(file);
    ^
    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?

    Thanks,

    -John
    Last edited by johnboy; 12-02-2011 at 05:32 AM. Reason: Fix arrows

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    LinkBacks (?)

    1. 04-05-2014, 10:52 PM

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •