比如:声明一个1024 字节的数组 ,传入一个Long 类型的数(占位8字节),如果我只读8个字节,会是什么情况呢?
有个例子:
import java.net.*;
import java.io.*;public class TestUDPServer{
public static void main(String[] args) throws Exception{
byte buf[] = new byte[1024];
DatagramSocket ds = new DatagramSocket (5678);
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
//ByteArrayInputStream bis = new ByteArrayInputStream(buf,0,dp.getLength());结果为0
DataInputStream dis = new DataInputStream(bis);
long n = dis.readLong();
System.out.println(n);
ds.close();
}
} 注释部分的写法结果为0 ,所以想了解下数组进行写和读的时候,是什么样的呢?
有个例子:
import java.net.*;
import java.io.*;public class TestUDPServer{
public static void main(String[] args) throws Exception{
byte buf[] = new byte[1024];
DatagramSocket ds = new DatagramSocket (5678);
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
//ByteArrayInputStream bis = new ByteArrayInputStream(buf,0,dp.getLength());结果为0
DataInputStream dis = new DataInputStream(bis);
long n = dis.readLong();
System.out.println(n);
ds.close();
}
} 注释部分的写法结果为0 ,所以想了解下数组进行写和读的时候,是什么样的呢?
解决方案 »
- 关于虚拟机的安装
- 请各位帮看一下这个小程序怎么运行不了
- 0 byte 的string 到底是个什么东西?
- 一个非常简单的排序问题,请指教
- 请教:JTable里面某一列加上JCheckBox后怎么成了单选框的效果?
- main都是public static void main?不能返回植吗
- interface Interaction {}和getFilePointer() 是做什么用的?
- 如何读写汉字和英文的混排? 在线等答案,马上结帖!
- jbuilder怎么发布war包到weblogic上呀
- 有关反编译的问题请教?
- public static void main (String[] args)各单词的意思
- jquery的问题
看看那个注释的构造函数中,是否有数组拷贝的代码。
我理解,
那个注释的构造函数中,会新生成一个数组做缓冲区。与参数不是同一个缓冲区。