java里面readall方法,无法解析方法readAllBytes()

What I’m trying to do

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[].

The problem

For an unknown reason my DataInputStream can't resolve the method readAllBytes().

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

}

}

XLRKr.png

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).

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'

}

Edit: Turns out as per the answers, Java 8 was insufficient and I should have used Java 9. Classic RTFM question.

解决方案

As per InputStream.readAllBytes() method javadoc this method was introduced in Java 9.

Since:

9

It won't work when the code is compiled or lower Java version, in your case Java 8.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `inputStream.readAllBytes()` 方法Java 9 中新增的方法,它可以读取 InputStream 中的所有字节,并将它们存储在一个 byte 数组中返回。在 Java 9 之前,我们可以使用一些其他的方式来读取 InputStream 中的所有字节,比如使用 Apache Commons IO 库中的 IOUtils 类的 `toByteArray` 方法,或者使用自己实现的读取方法。 使用 `inputStream.readAllBytes()` 方法可以简化代码,避免手动实现读取 InputStream 的逻辑。同时,由于该方法Java 9 中新增的,如果需要在 Java 9 之前的版本中使用,需要手动实现读取 InputStream 的逻辑。例如: ``` ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[1024]; while ((nRead = inputStream.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); byte[] bytes = buffer.toByteArray(); ``` 上面的代码中,使用 ByteArrayOutputStream 类来存储读取到的字节,并在读取完成后调用 `buffer.toByteArray()` 方法返回一个 byte 数组。这个方法比较通用,可以用于 Java 9 之前的所有版本。 ### 回答2: inputStream.readAllBytes() 是Java中InputStream类的一个方法,它的作用是将输入流中的所有字节读取并返回一个字节数组。 这个方法的使用非常简单,可以用来读取文件、网络数据、数据库中的BLOB等等。当想要一次性将输入流中的所有字节读取出来时,就可以使用这个方法。 使用 readAllBytes() 方法可以避免多次调用 read() 方法来读取字节并拼接成字节数组的麻烦,它是一次性将所有字节读取到一个字节数组中。 示例代码如下: ``` InputStream inputStream = new FileInputStream("example.txt"); byte[] bytes = inputStream.readAllBytes(); inputStream.close(); ``` 以上代码会读取名为 "example.txt" 的文件的所有字节,并将其存储到一个字节数组中。 需要注意的是,这个方法会一次性将所有字节读取到内存中,如果输入流中的字节太多,可能导致内存溢出。因此,在使用这个方法时需要考虑输入流的大小和系统的内存限制。 总而言之,inputStream.readAllBytes() 是一个方便的方法,可以快速将输入流中的所有字节读取并存储到一个字节数组中。 ### 回答3: inputStream.readAllBytes()是Java中的一个方法,它是在Java 9中引入的。这个方法用于从输入流中读取所有的字节,并将其存储在一个字节数组中。 使用这个方法,我们可以很方便地将输入流中的所有字节读取出来,并进行进一步的处理。与使用传统的输入流读取方式相比,它提供了更简洁的编程接口。 这个方法的返回值是一个字节数组,它包含了从输入流中读取的所有字节。如果输入流已经到达末尾,那么返回的字节数组的长度将为0。 使用这个方法时需要注意一些事项。首先,由于这个方法将输入流中的所有字节读取出来,所以如果输入流中的字节量很大,那么可能会导致内存溢出的问题。因此,在使用这个方法时需要确保输入流的字节量不会太大。 其次,这个方法只能用于读取字节流。如果需要读取字符流,可以使用InputStreamReader将字节流转换为字符流,然后使用字符流的读取方法来读取字符。 总之,inputStream.readAllBytes()是一个方便的方法,可以从输入流中读取所有的字节,并将其存储在一个字节数组中。但在使用这个方法时需要注意输入流的字节量不要太大,以避免内存溢出的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值