简体   繁体   English

Java NIO:为什么方法名称“读取”“写入”听起来相反?

[英]Java NIO: Why the method names “read” “write” sounds the opposite?

I am new to Java NIO and I am not an English native speaker. 我是Java NIO的新手,并且不会说英语。

When I read the method call about reading and writing buffer, I always get confused. 当我阅读有关读写缓冲区的方法调用时,总是感到困惑。 It sounds the opposite to me. 听起来与我相反。

For example, 例如,

fileChannel.read(buffer)

According to FileChannel javadoc, it means 根据FileChannel javadoc,这意味着

Reads a sequence of bytes from this channel into the given buffers 从该通道读取字节序列给定的缓冲区

I wonder why it does not call "write" instead. 我不知道为什么它不调用“ write”。

In English, "write into" and "read from" sounds a more natural pairs than "write from" and "read into". 用英语来说,“写入”和“读取”听起来比“写入”和“读取”更自然。 Also in code reading too 也在代码阅读中

fileChannel.write(buffer)

FileChannel writes a sequence of bytes into the given buffers FileChannel 写入字节序列给定的缓冲区

This clearly states what the actor is going to do. 这清楚地说明了演员将要做什么。

Now I need to read everything opposite in order to get thing right... 现在,我需要阅读相反的内容才能正确解决问题。

Maybe they call them like that because of historical reason how they call them in IO package? 也许由于历史原因,他们之所以这样称呼他们,是因为他们在IO套件中称呼它们为? Or maybe I am missing something obvious? 还是我缺少明显的东西? Hope I could have your advice how to interpret them correctly. 希望我能对您的建议给出正确解释的建议。

Thanks! 谢谢!

当您拥有object.verb(…) ,动词通常是在对象上执行的,因此,如果您想读取fileChannel ,则应具有fileChannel.read(…)

In any data transfer, there's always a source from which data is read , and a destination to which the data is written . 在任何数据传输中,总是有从中读取数据的和向其写入数据的目标 Perhaps because files are rather more permanent than memory buffer contents, it's just always been the convention to name transfer operations involving a file and a memory buffer with respect to the role of the file rather than that of the buffer. 也许是因为文件,而不是内存缓冲区的内容更持久,它只是一直公约涉及文件和内存缓冲区相对于该文件 ,而不是缓冲区的角色名称传输操作。 So when the file is the source and the buffer the destination, it's a "read" and when the file is the destination and the buffer the source, it's a "write". 因此,当文件是源而缓冲区是目标时,则是“读”;当文件是目标而缓冲区是源时,则是“写”。

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

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