0

This question already has an answer here:

I have this simple java code:

import java.nio.file.*;
import java.io.*;

public class Write {
   public static final String PATH = "/home/user/Desktop/hello.txt";

   public static void main(String[] argv) {
      Path path = Paths.get(PATH);
      OutputStream out;
      try {
         out = Files.newOutputStream(path, CREATE, APPEND); 
      } catch (IOException e) {
         System.out.println("Caught IOException: "+e.getMessage());
      }

   }
}

which wont compile due to this error:

Write.java:14: error: cannot find symbol
        out = Files.newOutputStream(path, CREATE, APPEND); 
                                          ^
symbol:   variable CREATE
location: class Write
Write.java:14: error: cannot find symbol
     out = Files.newOutputStream(path, CREATE, APPEND); 
                                               ^
symbol:   variable APPEND
location: class Write
2 errors

Usually this means that I've forgotten to import some stuff. I even tried to add:

import java.nio.file.StandardOpenOption 

But I get the same error.

EDIT: Ok so, I've solved my problem by following @rmlan advice. I did miss those constants. Thanks.

4 답변


3

Change your import from:

import java.nio.file.StandardOpenOption 

to

import static java.nio.file.StandardOpenOption.*

In order to reference the enum constants from the StandardOpenOption class as you have (without any qualification):

Files.newOutputStream(path, CREATE, APPEND); 

You must import all enum constants from that class statically (or at least statically import the specific ones that you are using).

Alternatively, as other answers have mentioned, you can keep the import you added, but in that case you must fully-qualify the references to the enum constants of StandardOpenOption:

Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND); 


0

You'll either have to make your import static

import static java.nio.file.StandardOpenOption.*;

or use the static reference from StandardOpenOptions

import java.nio.file.StandardOpenOption;

Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.APPEND);


0

These are static members, you have to either prepend the class they're in

StandardOpenOption.APPEND

or do a static import

import static java.nio.file.StandardOpenOption.APPEND;


0

Use this

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.APPEND;

or

import static java.nio.file.StandardOpenOption.*

instead of import java.nio.file.StandardOpenOption

Linked


Related

Latest