Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Strange errors with java.nio.file

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 0count; ++x)
            
    System.out.println("Element " " 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:11errormethod getName in interface Path cannot be applied to given types;
          
    System.out.println("File name is " filePath.getName());
                                                                   ^
      
    requiredint
      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.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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(READEXECUTE);
             
    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:13errorcannot find symbol
             filePath
    .checkAccess(READEXECUTE);
                     ^
      
    symbol:   method checkAccess(AccessMode,AccessMode)
      
    locationvariable filePath of type Path
    1 error 
    and


    PHP Code:
    PathDemo4.java:11errorcannot find symbol
              filePath
    .delete();
                      ^
      
    symbol:   method delete()
      
    locationvariable filePath of type Path
    1 error

     
    ----jGRASP wedge2: exit code for process is 1.
     
    ----jGRASPoperation 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.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Strange errors with java.nio.file

    Quote Originally Posted by Phynx View Post
    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?

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    PathDemo3.java:19: error: cannot find symbol
    filePath.checkAccess(READ,EXECUTE); 
            ^
      symbol:   method checkAccess(AccessMode,AccessMode)
      location: variable filePath of type Path
    1 error
    Last edited by Phynx; January 4th, 2012 at 12:09 PM.

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Strange errors with java.nio.file

    Can't see a checkAccess method anywhere in the API for nio Path.
    Last edited by newbie; January 4th, 2012 at 12:59 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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.

Similar Threads

  1. software can discover the errors of java codes ?
    By websit in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 1st, 2011, 08:34 PM
  2. [SOLVED] Very strange results when reading from a test file
    By DougFane in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 06:30 PM
  3. hi - Java swing errors
    By genlastudio in forum AWT / Java Swing
    Replies: 1
    Last Post: March 22nd, 2011, 05:49 PM
  4. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM
  5. miniBuilder on Linux- java errors
    By nwtjv in forum Exceptions
    Replies: 0
    Last Post: March 25th, 2010, 06:54 AM

Tags for this Thread