Results 1 to 3 of 3
  1. #1
    jazzgnat is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default 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:
    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);
    	}
    	}
    }
    the error is:
    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

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

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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

  1. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  2. Having Trouble with a GUI Object class
    By jrdarkness in forum New To Java
    Replies: 4
    Last Post: 11-18-2011, 01:30 AM
  3. FileChannel
    By dewitrydan in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 06:38 AM
  4. trouble with a class
    By Alexander Montero in forum New To Java
    Replies: 2
    Last Post: 06-17-2009, 10:01 PM
  5. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 02:55 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
  •