Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-02-2011, 04:30 AM   PM User | #1
johnboy
New to the CF scene

 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
johnboy is an unknown quantity at this point
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 04:32 AM.. Reason: Fix arrows
johnboy is offline   Reply With Quote
Old 12-02-2011, 06:06 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:41 AM.


Advertisement
Log in to turn off these ads.