简体   繁体   English

IntelliJ 无法识别内置的 Java 类/函数

[英]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.我想创建一个 InputStream 并将结果写入输出流,最简单的方法是通过 Files.copy() 但不幸的是,我的 IntelliJ 无法识别任何这些方法。 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.但是Files.copy()不是变量声明,所以它在这个地方不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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