Results 1 to 12 of 12
- 01-03-2012, 12:29 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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
the error i receive.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"); } } }
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.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
- 01-03-2012, 01:28 AM #2
Re: Can't compile if using anything from java.nio package
Aren't READ and EXECUTE supposed to be strings or static fields?
- 01-03-2012, 04:56 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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.
and the error i get from itJava 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); } } }
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
- 01-03-2012, 05:03 PM #4
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
- 01-03-2012, 05:20 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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?
- 01-03-2012, 05:25 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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
- 01-03-2012, 05:57 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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
- 01-03-2012, 06:09 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 01-03-2012, 06:13 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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
- 01-04-2012, 09:43 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 01-04-2012, 05:14 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
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.
- 01-04-2012, 08:00 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
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
-
Is it possible to compile Java-code when running a Java-program?
By Toll in forum Advanced JavaReplies: 13Last Post: 06-29-2011, 07:25 AM -
run package inside anthor package
By AhmedAdel in forum AWT / SwingReplies: 4Last Post: 04-20-2010, 11:52 AM -
.java won't compile
By paul21 in forum New To JavaReplies: 4Last Post: 03-30-2010, 06:58 AM -
The declared package does not match the expected package
By oneforall in forum EclipseReplies: 7Last Post: 11-09-2009, 07:51 AM -
Compile and Run a Java package
By hussain in forum New To JavaReplies: 1Last Post: 09-10-2009, 09:15 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks