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 6 of 6

Thread: BufferedOutputStream and FileChannel

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default BufferedOutputStream and FileChannel

    I'm creating a program to create and write to a file for homework, but it just won't compile. Everything looks okay, so I don't understand what's happening.

    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 phoneList =
    			Paths.get("C:\\Users\\Rayne\\Documents\\School Work\\CIST 2372\\PhoneList.txt");
     
    		String delimiter = ",";
    		String s = "000-000-000,FFFFFFFFFFF,LLLLLLLLLL" + System.getProperty("line.separator");
    		final int RECSIZE = s.length();
    		FileChannel fc = null;
    		String phoneNumber;
    		String firstName;
    		String lastName;
    		final String QUIT = "999";
     
    		createEmptyFile(phoneList, s);
     
    		try
    		{
    			fc = (FileChannel)phoneList.newByteChannel
    				(CREATE, WRITE);
    			System.out.print("Enter phone number in 000-000-000 format or " + QUIT + " to quit >> ");
    			phoneNumber = input.nextLine();
    			while(!(phoneNumber.equals(QUIT)))
    			{
    				System.out.print("Enter first name for phone number " + phoneNumber + " >> ");
    				firstName = input.nextLine();
    				System.out.print("Enter last name >> ");
    				lastName = input.nextLine();
    				s = phoneNumber + delimiter + firstName + delimiter +
    				lastName + System.getProperty("line.separator");
    				byte[] data = s.getBytes();
    				ByteBuffer buffer = ByteBuffer.wrap(data);
    				fc.write(buffer);
    				System.out.print("Enter next phone number or " + QUIT + " to quit >> ");
    				phoneNumber = input.nextLine();
    			}
    			fc.close();
    		}
    		catch (Exception e)
    		{
    			System.out.println("Error message: " + e);
    		}
    	}
    	public static void createEmptyFile(Path file, String s)
    	{
    		final int NUMRECS = 100;
     
    		try
    		{
    			OutputStream outputStr = new 
    				BufferedOutputStream(file.newOutputStream(CREATE));
    			BufferedWriter writer = new BufferedWriter(new
    				OutputStreamWriter(outputStr));
    			for(int count = 0; count < NUMRECS; ++count)
    				writer.write(s, 0, s.length());
    				writer.close();
    		}
    		catch (Exception e)
    		{
    			System.out.println("Error message: " + e);
    		}	
    	}
    }

    The error messages I get are:

    error: cannot find symbol
    symbol: method newByteChannel (StandardOpenOption, StandardOpenOption)
    location: variable phoneList of type Path

    error: cannot find symbol
    symbol: method newOutputStream (StandardOpenOption)
    location: variable file of type Path


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: BufferedOutputStream and FileChannel

    Are those methods defined for the Path class?

    Look in the API doc to see what class those methods belong to.
    Last edited by Norm; March 10th, 2012 at 08:10 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    CameronRayne (March 10th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BufferedOutputStream and FileChannel

    From what the document tells me, is that BufferedOutputStream and ByteChannel are each classes themselves, but in each example that my book gives me, they are written they way I have them in my code. That's what's confusing me.

    EDIT: I figured out the problem. The examples in my book are wrong. I found the exact same examples on another site, and they're written differently. Once I wrote them the way that they were on the site, the program complied and worked just fine. Thanks for your help though!
    Last edited by CameronRayne; March 10th, 2012 at 01:03 PM. Reason: Solved the problem

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: BufferedOutputStream and FileChannel

    What about the methods in the error messages. Did you look them up to see what classes they were in?
    The compiler does not think that they are in the Path class. What class are they in?

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BufferedOutputStream and FileChannel

    The methods were written the wrong way, that's why they couldn't be found, because they were calling something that didn't exist due to a typo in my book for that chapter, and since I'm just learning it, I didn't know differently.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: BufferedOutputStream and FileChannel

    When you get errors like those, look in the API doc and find what classes the methods are in.