0

我是 Java 编程语言的新手,事实上我只有一些基本的编程概念。我遇到了以下一段 Java 代码,但我无法理解其中的函数。如果有人向我解释'outData.writeInt()'做什么以及如何使用它,我将不胜感激?谢谢你。

 try {
    if(moe.getSource() == forward)outData.writeInt(1);
    if(moe.getSource() == reverse)outData.writeInt(2);
    if(moe.getSource() == leftTurn)outData.writeInt(3);
    if(moe.getSource() == rightTurn)outData.writeInt(4);
    if(moe.getSource() == speedUp)outData.writeInt(6);
    if(moe.getSource() == slowDown)outData.writeInt(7);

    outData.flush(); 
    }
 catch (IOException ioe) {
    System.out.println("\nIO Exception writeInt");
 }
4

2 回答 2

2

一个快速的谷歌给了我DataOutputStream.writeInt()的文档

将一个 int 作为四个字节写入底层输出流,先是高字节。如果没有抛出异常,则写入的计数器增加 4

DataOutputStream将写入文件,或者可能是网络连接。

于 2012-08-14T12:59:53.977 回答
2

writeInt()记录在 DataOutputStream

将一个 int 作为四个字节写入底层输出流,先是高字节。如果没有抛出异常,则写入的计数器增加 4。

int简单来说,它以大端写入一个 32 位值。


如果你想知道一个方法是做什么的,最好从源代码开始

/**
 * Writes an <code>int</code> to the underlying output stream as four
 * bytes, high byte first. If no exception is thrown, the counter
 * <code>written</code> is incremented by <code>4</code>.
 *
 * @param      v   an <code>int</code> to be written.
 * @exception  IOException  if an I/O error occurs.
 * @see        java.io.FilterOutputStream#out
 */
public final void writeInt(int v) throws IOException {
    out.write((v >>> 24) & 0xFF);
    out.write((v >>> 16) & 0xFF);
    out.write((v >>>  8) & 0xFF);
    out.write((v >>>  0) & 0xFF);
    incCount(4);
}
于 2012-08-14T13:00:12.270 回答