问题描述
我正在尝试构建一个java.net应用程序,在该应用程序中,客户端和服务器必须通过序列化的收集类型(如byte[]
)相互发送数据.
I'm trying to build a java.net application where client and server have to send data to each other via serialised collection types as byte[]
.
由于未知原因,我的DataInputStream无法解析方法readAllBytes()
.
For an unknown reason my DataInputStream can't resolve the method readAllBytes()
.
一个朋友把它扔到了他的IDE中,它没有抱怨.我不确定这可能是版本问题,但是我检查了一下,但没有配置错误.我正在使用Java 8.
A friend threw it in his IDE and it didn't complain. I'm unsure how this could be a version issue, but I checked and I didn't misconfigure my project. I'm using 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();
}
}
我实际上确定应该支持此方法,但是我不知道为什么不支持该方法.因为它被列为方法,并且继承自Input Stream(docs.oracle.com).
I'm actually sure this method should be supported, but I can't figure out why it isn't since it is listed as a method, inherited from Input Stream (docs.oracle.com).
包含方法的项目也是Gradle Project.因此,为了完整显示图片,这是 build.gradle :
The project, containing the method is also a Gradle Project. Hence, to complete the picture, this is the 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不足,我应该使用Java9.经典RTFM问题.
Turns out as per the answers, Java 8 was insufficient and I should have used Java 9. Classic RTFM question.
推荐答案
按照 InputStream.readAllBytes()
方法javadoc此方法是Java 9中引入的.
As per InputStream.readAllBytes()
method javadoc this method was introduced in Java 9.
9
在编译代码或更低版本的Java版本(在您的情况下为Java 8)时,它将无法正常工作.
It won't work when the code is compiled or lower Java version, in your case Java 8.
这篇关于无法解析方法readAllBytes()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!