Results 1 to 3 of 3
- 03-11-2012, 05:25 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
I'm having trouble with the FileChannel class
I have been working on a program to write a phone list for school but I keep getting an error when trying to compile. I've been going by the books method and I even tried copying an example program from the book but it also failed to compile. While researching this it became apparent that there is an error in the text but I can't find an answer as to how it is corrected.
This is my code thus far:the error is:Java Code:import java.nio.file.*; import java.io.*; import java.nio.channels.FileChannel; import java.nio.ByteBuffer; import static java.nio.file.StandardOpenOption.*; import java.util.Scanner; import java.text.*; public class WritePhoneList { public static void main(String[] args) { Scanner input = new Scanner(System.in); Path file = Paths.get("C:\\MyClasses\\phone_list.txt"); FileChannel fc = null; String firstName; String lastName; String phoneNum; String delimiter = " , "; final String QUIT = "999"; String s = " , , 000-000-0000 "+"\n"; String add = null; final int RECSIZE = s.length(); System.out.println("Enter the contact's first name or 999 to quit"); firstName = input.nextLine(); final int NUMRECS = 50; int x = 0; try { OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output)); for(int count = 0; count<NUMRECS; ++count) writer.write(s,0,s.length()); writer.close(); while(!(firstName.equals(QUIT))) { ++x; OutputStream writing = new BufferedOutputStream(Files.newOutputStream(file, CREATE)); BufferedWriter push = new BufferedWriter(new OutputStreamWriter(writing)); System.out.println("Enter the contact's last name."); lastName = input.nextLine(); System.out.println("Enter the contact's phone number."); phoneNum = input.nextLine(); add = firstName + delimiter + lastName + delimiter + phoneNum + System.getProperty("line.Separator"); System.out.println("Enter the contact's first name or 999 to quit"); firstName = input.nextLine(); fc = (FileChannel)file.newByteChannel(READ, WRITE) fc.postion(s.length()*x); push.write(add,0,add.length()); push.close(); } } catch(Exception e) { System.out.println("Error Message: "+e); } } }
WritePhoneList.java:56: error: cannot find symbol
fc = (FileChannel)file.newByteChannel(READ, WRITE);
^
symbol: method newByteChannel(StandardOpenOption,StandardOpenOpti on)
location: variable file of type Path
WritePhoneList.java:57: error: cannot find symbol
fc.postion(s.length()*x);
^
symbol: method postion(int)
location: variable fc of type FileChannel
2 errors
Any help with this would be appreciated. I know its rough but I just can't get past this FileChannel thing.Last edited by Norm; 03-11-2012 at 06:40 PM. Reason: added code tags
- 03-11-2012, 06:43 PM #2
Re: I'm having trouble with the FileChannel class
The code you posted does NOT match the error messages.
Please post the code that generated the error messages.
For the errors you show, are the methods that are being used in the class of the objects they are referencing?
Read the API doc to make sure.Last edited by Norm; 03-11-2012 at 07:00 PM.
-
Re: I'm having trouble with the FileChannel class
It is the code that generated the error messages.
Regarding the first error, on line 56, jazzgnat attemps to use a method:
file.newByteChannel(READ, WRITE);
The variable 'file' is defined on line 15 as a 'Path' object. 'READ' and 'WRITE' relate to StandardOpenOption.
The compiler is complaining that the method: Path.newByteChannel(StandardOpenOption, StandardOpenOption) does not exist, which if you check the java docs - it doesn't.
The closest method I can find in the java docs is in the package: java.nio.files.Files.newByteChannel(Path path, OpenOption... options), which returns a SeekableByteChannel which is an interface implemented by FileChannel.
It's a static method so you should change your code to this:
Java Code:// fc = (FileChannel) file.newByteChannel(READ, WRITE); fc = Files.newByteChannel(file, READ, WRITE);
Similar Threads
-
Trouble accessing outer class variable from Inner class
By Javasubbu in forum New To JavaReplies: 5Last Post: 12-18-2011, 04:06 AM -
Having Trouble with a GUI Object class
By jrdarkness in forum New To JavaReplies: 4Last Post: 11-18-2011, 01:30 AM -
FileChannel
By dewitrydan in forum New To JavaReplies: 3Last Post: 10-29-2010, 06:38 AM -
trouble with a class
By Alexander Montero in forum New To JavaReplies: 2Last Post: 06-17-2009, 10:01 PM -
Having trouble understanding Class Graphic
By Bernard Robitaille in forum JCreatorReplies: 1Last Post: 04-18-2009, 02:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks