Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Can't compile if using anything from java.nio package

  1. #1
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Can't compile if using anything from java.nio package

    So i've been following a learning guide and have just started learning about the file input/output.
    The issue I have is that I get errors when i try to compile if using anything from the java.nio package.
    currently using jdk 1.7.0_02

    an example of a program that won't compile
    Java Code:
    import java.io.*;
    import java.nio.file.*;
    
    public class PathDemo2
    {
    	public static void main(String[] args)
    	{
    		Path dir = Paths.get("C:\\Java\\Chapter.13\\Data.txt");
    		System.out.println("Path is " + dir.toString());
    		try
    		{
    			dir.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");
    		}
    	}
    }
    the error i receive.
    Java Code:
    PathDemo2.java:12: error: cannot find symbol
    			dir.checkAccess(READ, EXECUTE);
    			                ^
      symbol:   variable READ
      location: class PathDemo2
    PathDemo2.java:12: error: cannot find symbol
    			dir.checkAccess(READ, EXECUTE);
    			                      ^
      symbol:   variable EXECUTE
      location: class PathDemo2
    2 errors
    anyone have any idea what i'm doing wrong? even a direct copy/paste of the code from the learning guide has the same issue.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Can't compile if using anything from java.nio package

    Aren't READ and EXECUTE supposed to be strings or static fields?

  3. #3
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    if they are, the guide doesn't say anthing about it.

    here's another program that doesn't compile either.
    Java Code:
    import java.nio.file.*;
    import java.io.*;
    import static java.nio.file.StandardOpenOption.*;
    
    public class FileOut
    {
    	public static void main(String[] args)
    	{
    		Path file = Paths.get("C:\\Java\\Chapter.13\\Grades.txt");
    		String s = "ABCDF";
    		byte[] data = s.getBytes();
    		OutputStream output = null;
    		try
    		{
    			output = new BufferedOutputStream(file.newOutputStream(CREATE));
    			output.write(data);
    			output.flush();
    			output.close();
    		}
    		catch(Exception e)
    		{
    			System.out.println("Message: " + e);
    		}
    	}
    }
    and the error i get from it
    Java Code:
    FileOut.java:15: error: cannot find symbol
    			output = new BufferedOutputStream(file.newOutputStream(CREATE));
    			                                      ^
      symbol:   method newOutputStream(StandardOpenOption)
      location: variable file of type Path
    1 error
    
    > Process Exit Code: 1
    > Time Taken: 00:00

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Can't compile if using anything from java.nio package

    I'm looking at Path (More New I/O APIs for the Java™ Platform)

    and I don't see that "newOutputStream(CREATE)" is a valid method for the Path interface. What reference are you basing this code on, and what exactly are you trying to accomplish? ... Because, file IO is built into java and doesn't require the bio packages :D

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Can't compile if using anything from java.nio package

    I was just looking at the Path API and there also isn't a checkAccess() method is there?

  6. #6
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    The code is based on what im reading in "java programming 6th edition by joyce farrell"
    Java Programming, 6th Edition | Wow! eBook - Blog

    im on chapter 13, and the code that i previously posted was directly from the book

  7. #7
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    The code i previously posted is based on whats written in "java programming, 6th edition by joyce farrell"

    the newOutputSteam is in java.nio.file.Files

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Can't compile if using anything from java.nio package

    OK.
    But you are calling that method, not on Files, but on an object of class Path.

    So I think "based on" is the important bit here.

    So, what did the original you based this off actually say?
    Same goes for the code in the original post.

  9. #9
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    the original code is exactly as i posted in the original post and the post after that one, they are exercises, i always write them and compile them while i read it to learn the language

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Can't compile if using anything from java.nio package

    Then bin the book because it is clearly wrong.

    Just look at the links to the API as posted here to see that that code (as you posted) is not correct and cannot compile.
    quad64bit likes this.

  11. #11
    Phynx is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    alright, thanks :) i'll just look up tutorials for the io and nio, the other stuff in the book works thus far.

  12. #12
    DoctorV is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: Can't compile if using anything from java.nio package

    The closest working code that corresponds to the offending line would be:

    output = new BufferedOutputStream(Files.newOutputStream(file,CR EATE));

    This works, but appears to open existing files in overwrite mode - not ideal.
    Last edited by DarrylBurke; 01-08-2012 at 09:12 AM. Reason: Blog link removed

Similar Threads

  1. Replies: 13
    Last Post: 06-29-2011, 07:25 AM
  2. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  3. .java won't compile
    By paul21 in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 06:58 AM
  4. Replies: 7
    Last Post: 11-09-2009, 07:51 AM
  5. Compile and Run a Java package
    By hussain in forum New To Java
    Replies: 1
    Last Post: 09-10-2009, 09:15 AM

Posting Permissions

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