robertben
asked on
Problem with IO java class method, newOutputStream(CREATE), program won't compile
Hello Experts,
I am getting a compilation error with the program below. I don't understand why because the code is straight from the book. The problem according to Eclipse is the newOutStream() method which is supposed to come from the java.nio.file.StandardOpen Option.* class, which as you can see is imported into the file. The error hint states; "The method newOutputStream(StandardOp enOption) is undefined for the type Path". Help needed please. I hope I provided enough info.
I am getting a compilation error with the program below. I don't understand why because the code is straight from the book. The problem according to Eclipse is the newOutStream() method which is supposed to come from the java.nio.file.StandardOpen
import java.nio.file.*;
import java.io.*;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class CreateEmptyEmployeesFile
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\\JavaPrograms\\Data.txt");
String s = "000, ,00.00" + System.getProperty("line.separator");
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
final int NUMRECS = 1000;
try
{
OutputStream output = new BufferedOutputStream(file.newOutputStream(CREATE));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
for(int count = 0; count < NUMRECS; ++count)
writer.write(s, 0, s.length());
writer.close();
}
catch(Exception e)
{
System.out.println("Error message: " + e);
}
}
}
ASKER
That worked great. I don,t understand why there is any problem with the code to begin with. It came straight out of the Joyce Farrell, Java Programming book. There is another similar problem program. The code is listed below. I tried to modify the statement similar to the way you presented the solution to the earlier problem, but it did not work. Eclipse is giving me an error message for the checkAccess(READ, EXECUTE) method. The error message is: "The method checkAccess(AccessMode, AccessMode) is undefined for the type Path". I am not sure if I should start another thread for this. I increased the points
package pathDemos;
import java.nio.file.*;
import static java.nio.file.AccessMode.*;
import java.io.IOException;
public class PathDemo3
{
public static void main(String[] args)
{
Path filePath = Paths.get("C:\\JavaPrograms\\Data.txt");
System.out.println("Path is " + filePath.toString());
try
{
filePath.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");
}
}
}
Instead of using
>why there is any problem with the code to begin with. It came straight out of the Joyce Farrell, Java Programming book
Google says
that book was published in Feb 2011
that Java 7 waslaunched on July 2007
I guess a lot of the API was changed in those 3+ years.
filePath.checkAccess(READ, EXECUTE);
you should use
filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
>why there is any problem with the code to begin with. It came straight out of the Joyce Farrell, Java Programming book
Google says
that book was published in Feb 2011
that Java 7 waslaunched on July 2007
I guess a lot of the API was changed in those 3+ years.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
error above
Google says
that book was published in Feb 2007
that Java 7 was launched on July 2011
Yes, I was wandering about that.
Thanks. I will probably need more help with this chapter, but I'll start a new post.
Google says
that book was published in Feb 2007
that Java 7 was launched on July 2011
Yes, I was wandering about that.
Thanks. I will probably need more help with this chapter, but I'll start a new post.
Open in new window
try usingOpen in new window