简体   繁体   中英

IntelliJ does not recognize built-in java classes/functions

I want to create an InputStream and write the result to an outputstream, the easiest way to do this is by Files.copy() But unfortunately, my IntelliJ does not recognize any of those methods. Why?

在此处输入图片说明

在此处输入图片说明

Your code is not within a method, but directly within your class definition:

public class RequestHandler {  // DOES NOT COMPILE
  File file = new File("output.txt");

  Files.copy(....);
}

The definition of the file variable actually compiles as it looks like a member definition of the class. But the Files.copy() is not a variable declaration, and so it does not work in this place.

Add a function, and it should work, for example:

public class RequestHandler {
  File file = new File("output.txt");

  public void copyToOutput(InputStream in) {
    Files.copy(in, new FileOutputStream(file));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM