代码之家  ›  专栏  ›  技术社区  ›  blkpingu aaquib

无法解析方法readAllBytes()。

  •  2
  • blkpingu aaquib  · 技术社区  · 6 年前

    我在做什么

    我正在尝试构建一个java.net应用程序,在该应用程序中,客户机和服务器必须通过串行收集类型互相发送数据,如下所示 byte[] .

    我想做的

    由于未知原因,我的数据输入流无法解析该方法 readAllBytes() .

    一个朋友把它扔到了他的侧面,没有抱怨。我不确定这怎么可能是一个版本问题,但我检查了并且没有错误地配置我的项目。我使用的是Java 8。

      public void startClient() {
            try {
                Socket client = new Socket("localhost", 7000);
                DataOutputStream out = new DataOutputStream(client.getOutputStream());
                out.writeUTF("Hi i'm " + client.getLocalSocketAddress());
                DataInputStream input = new DataInputStream(client.getInputStream());
                byte[] sent = input.readAllBytes(); //"can't resolve method 'readAllBytes()'
                getDataFromClient(input.readAllByes());
    //"can't resolve method 'readAllBytes()'
                client.close();
    
    
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    enter image description here

    实际上,我确信应该支持这个方法,但我不明白为什么不支持它。 It's listed as a method, inherited from Input Stream in Oracles Docs .

    这也是一个渐变项目,为了完整性,这是build.gradle:

    plugins {
        id 'java'
        id 'application'
    }
    
    sourceSets {
        main {
            resources {
                srcDirs = ["src/main/java"]
                includes = ["**/*.fxml","**/*.png"]
            }
        }
    }
    
    group 'prog3'
    version '0.1'
    mainClassName='Client'
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
        testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
    }
    

    编辑: 根据答案,Java 8是不够的,我应该使用Java 9。经典的RTFM问题。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Karol Dowbecki    6 年前

    按照 InputStream.readAllBytes() 方法JavaDoc是在Java 9中引入的。

    因为:

    当代码编译或Java版本较低时,它将不起作用,在Java Java 8中。