Strange errors with java.nio.file
Hello, I am learning some basic file operations in Java and running into a strange error. Code in my Java textbook is shown below:
PHP Code:
import java.nio.file.*;
public class PathDemo
{
public static void main(String[] args)
{
Path filePath =
Paths.get("c:\\Java\\Chapter.13\\Data.txt");
int count = filePath.getNameCount();
System.out.println("Path is " + filePath.toString());
System.out.println("File name is " + filePath.getName());
System.out.println("There are " + count +
" elements in the file path");
for(int x = 0; x < count; ++x)
System.out.println("Element " + x + " is " +
filePath.getName(x));
}
}
I am using jGRASP (Java SDK 1.7 installed) and when I try to compile I am getting an error
PHP Code:
PathDemo.java:11: error: method getName in interface Path cannot be applied to given types;
System.out.println("File name is " + filePath.getName());
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
I am really at loss on what I am doing wrong. Any help would be appreciated.
Re: Strange errors with java.nio.file
By looking at the error description, getName() takes integer arguments and it can not find any argument. It's implemented with taking an integer argument.
Re: Strange errors with java.nio.file
Thank you so much! Giving it a integer parameter does work
PHP Code:
System.out.println("File name is " + filePath.getName(0));
another errors that might be related to nio class
PHP Code:
import java.nio.file.*;
import static java.nio.file.AccessMode.*;
import java.io.IOException;
public class PathDemo3
{
public static void main(String[] args)
{
Path filePath =
Paths.get("C:\\Java\\Chapter.13\\Data.txt");
System.out.println("Path is " + filePath.toString());
try
{
filePath.checkAccess(READ, EXECUTE);
System.out.println("File can be read and executed");
}
catch(IOException e)
{
System.out.println("File cannot be used for this application");
}
}
}
and
PHP Code:
import java.nio.file.*;
import java.io.IOException;
public class PathDemo4
{
public static void main(String[] args)
{
Path filePath =
Paths.get("C:\\Java\\Chapter.13\\Data.txt");
try
{
filePath.delete();
System.out.println("File or directory is deleted");
}
catch (NoSuchFileException e)
{
System.out.println("No such file or directory");
}
catch (DirectoryNotEmptyException e)
{
System.out.println("Directory is not empty");
}
catch (IOException e)
{
System.out.println("No permission to delete");
}
}
}
return
PHP Code:
PathDemo3.java:13: error: cannot find symbol
filePath.checkAccess(READ, EXECUTE);
^
symbol: method checkAccess(AccessMode,AccessMode)
location: variable filePath of type Path
1 error
and
PHP Code:
PathDemo4.java:11: error: cannot find symbol
filePath.delete();
^
symbol: method delete()
location: variable filePath of type Path
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
it sure looks like I am missing an include, but the only thing that is confusing that those two programs are in my "Java Programming by Joyce Farrell. 6th edition" textbook and according to author should compile without an error.
Re: Strange errors with java.nio.file
I'm having that same issue using the same book, how did you manage to get past it?
Re: Strange errors with java.nio.file
Quote:
Originally Posted by
Phynx
I'm having that same issue using the same book, how did you manage to get past it?
Did you include Path file in your project?
Re: Strange errors with java.nio.file
Nope, since i wrote the code exactly as its shown in the book, i'll include path and see if it goes through
Re: Strange errors with java.nio.file
well didn't make a difference, i imported the following..
import java.nio.file.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.AccessMode;
import static java.nio.file.AccessMode.*;
import java.nio.file.spi.FileSystemProvider;
import java.io.IOException;
import java.io.File;
still receive this error.
Code :
PathDemo3.java:19: error: cannot find symbol
filePath.checkAccess(READ,EXECUTE);
^
symbol: method checkAccess(AccessMode,AccessMode)
location: variable filePath of type Path
1 error
Re: Strange errors with java.nio.file
Can't see a checkAccess method anywhere in the API for nio Path.
Re: Strange errors with java.nio.file
java.nio.file.spi.FileSystemProvider has an checkAccess method
FileSystemProvider (Java Platform SE 7 )
which is why i imported it, but it didn't make a difference, so it may be a mistake written in the book which is a shame since this book doesn't have much for the older java.io package.
Re: Strange errors with java.nio.file
But you have a Path object, which doesn't seem to extend the abstract class FileSystemProvider, so the method will be unavaliable to a Path object.