Solved

Problem with IO java class method, newOutputStream(CREATE), program won't compile

Posted on 2012-03-10
5
1,097 Views
Last Modified: 2012-03-10
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.StandardOpenOption.* class, which as you can see is imported into the file. The error hint states; "The method newOutputStream(StandardOpenOption) is undefined for the type Path". Help needed please. I hope I provided enough info.

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);
      }

   }
}

Open in new window

0
Comment
Question by:robertben
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 37706024
Instead of using
OutputStream output = new BufferedOutputStream(file.newOutputStream(CREATE));

Open in new window

try using
OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));

Open in new window

0
 

Author Comment

by:robertben
ID: 37706067
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");
      }
   }
}

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 37706118
Instead of using
filePath.checkAccess(READ, EXECUTE);

Open in new window

you should use
filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);

Open in new window

 
>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.
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 37706127
error above  
Google says
 that book was published in Feb 2007  
that Java 7 was launched on July  2011
0
 

Author Closing Comment

by:robertben
ID: 37706134
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 66
session migration servlets 2 36
oracle 11g 23 82
servlet example 11 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question