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.
Code Java:
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
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.
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!
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?
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.
Re: BufferedOutputStream and FileChannel
When you get errors like those, look in the API doc and find what classes the methods are in.