62,614
社区成员
发帖
与我相关
我的任务
分享
InputStream is = this.getClass().getResourceAsStream("/res/a.jpg");
byte[] b = new byte[is.available()];
for(;is.available()>0;i++){
b[i] = (byte)is.read();
}
ImagIcon image = new ImageIcon(b);
InputStream is = this.getClass().getResourceAsStream("/res/a.jpg");
byte[] b = new byte[is.available()];
is.read(b);
ImagIcon image = new ImageIcon(b);
ImageIcon image = new ImageIcon(this.getClass().getResource("/res/a.png"));
能够正常运行.
for(;is.available()>0;i++){
int m = is.read();
b[i] = (byte) m;
}
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read(); //循环读取
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) { //估计是这里有异常抛出
}
return i;
}
InputStream is = this.getClass().getResourceAsStream("/res/a.jpg");
System.out.println("文件大小/is.available():"+is.available());
int length = 30000;
byte[] b = new byte[length];
int i = 0;
for(;is.available()>0;i++){
int m = is.read();
b[i] = (byte) m;
}
System.out.println("用For循环读取字节数为: "+i);
System.out.println("--------用read(byte[])方法读取-------------------");
is = this.getClass().getResourceAsStream("/res/a.jpg");
b = new byte[length];
i = is.read(b);
System.out.println("用read共读取字节数为: "+i);
System.out.println("--------用read(byte[],int,int)方法读取-----------");
is = this.getClass().getResourceAsStream("/res/a.jpg");
BufferedInputStream bis = new BufferedInputStream(is);
b = new byte[length];
i = bis.read(b,0,b.length);
System.out.println("用read(b,int,int)共读取字节数为: "+i);
byte[] b = new byte[length];
int i = 0;
int c = -1;
while((c = is.read())!=-1){//不考虑性能等其它事情.
b[i++] = (byte)c;
}
System.out.println("用while循环读取字节数为: "+i);
//文件大小/is.available():27446
//用while循环读取字节数为: 27446
System.out.println("\n--------用read(byte[])方法读取-------------------");
is = this.getClass().getResourceAsStream("/res/a.jpg");
b = new byte[length];
i = is.read(b);
System.out.println("用read共读取字节数为: "+i);
//--------用read(byte[])方法读取-------------------
//用read共读取字节数为: 8497
InputStream is = this.getClass().getResourceAsStream("/res/a.jpg");
System.out.println("文件大小/is.available():"+is.available());
int length = 30000;
byte[] b = new byte[length];
int i = 0;
int c = -1;
while((c = is.read())!=-1){ //只是例子没有优化.
b[i++] = (byte)c;
}
System.out.println("用while循环读取字节数为: "+i);
//文件大小/is.available():27446
//用while循环读取字节数为: 27446
System.out.println("\n--------用read(byte[])方法读取-------------------");
is = this.getClass().getResourceAsStream("/res/a.jpg");
//-------------------重点看这里!!!!!----------------
b = new byte[length];
i = is.read(b);
System.out.println("用read共读取字节数为: "+i);
//--------用read(byte[])方法读取-------------------
//用read共读取字节数为: 8497
System.out.println("\n--------用read(byte[],int,int)方法读取-----------");
is = this.getClass().getResourceAsStream("/res/a.jpg");
BufferedInputStream bis = new BufferedInputStream(is);
//-------------------重点看这里!!!!!----------------
b = new byte[length];
i = bis.read(b,0,b.length);
System.out.println("用read(b,int,int)共读取字节数为: "+i);
//--------用read(byte[],int,int)方法读取-----------
//用read(b,int,int)共读取字节数为: 27446
//学习