浏览 2343 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2005-03-17
都是返回整形。但是前一个返回的整形是可以转化成字节的整型。后一个返回的整型是什么呢?我看了看,好像是一次读取了多少字节的数目。但是。单单是返回这的话。那我用read(byte[] buf,int off, int len)从输入流里得到数据并写入输出流。为什么输出流得到了数据流得数据。输入流并没有返回真正的字节阿
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2005-03-17
还有。我做的实验。把一个文本文件读入输入流。然后输出流是一个ByteArrayOutputStream .我每读一个输入流 。然后把输入流返回的结果写入输出流。然后我马上把输出流里所有的内容变成字符串从屏幕中打印出来。如果我是用的read()方法。那么我打出的是文本文档里的数据。如果我使用的方法是read(byte[] buf)或者是read(byte[] buf,int off,int len)那么屏幕中打出的是'口'字。一个又一个的'口'字。这是为什么呢?
|
|
| 返回顶楼 | |
|
时间:2005-03-17
说那么多,还不如贴代码上来看看
这样最直接,最有效 |
|
| 返回顶楼 | |
|
时间:2005-03-17
this
<code> public synchronized int read() { return (pos < count) ? (buf[pos++] & 0xff) : -1; } </code> <code> public synchronized int read(byte b[], int off, int len) { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } if (pos >= count) { return -1; } if (pos + len > count) { len = count - pos; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; } </code> <code> public synchronized int read(byte b[], int off, int len) { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } if (pos >= count) { return -1; } if (pos + len > count) { len = count - pos; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; } </code> |
|
| 返回顶楼 | |
|
时间:2005-03-17
这是JDK的代码,你调用的代码?
|
|
| 返回顶楼 | |
|
时间:2005-03-18
package iotransfer ;
import java.io.*; import java.util.zip.*; public class zipTest { public zipTest () { } public static void main(String[] args) throws IOException { InputStream in= new FileInputStream("1.txt"); ByteArrayOutputStream out = new ByteArrayOutputStream(); int num; byte[] buf = new byte[1]; int a=in.read(buf); out.write(a); String str=new String(out.toByteArray()); System.out.println(str); a=in.read(buf); out.write(a); str = new String(out.toByteArray()); System.out.println(str); out.close(); in.close(); } } this is my code~ |
|
| 返回顶楼 | |
|
时间:2005-03-19
第一个方法返回的是int,第2个方法返回的是byte数组,所以你要用第2个的时候,你要先把byte转成int,这样才能正确显示.
/** * 将 byte 数组转化成整数 * @param b 字节数组 * @param iFrom 开始转换位置 * @param iLen 要转换的字节数 * @return 转换后结果 */ public static int b2i(byte[] b ,int iFrom,int iLen) { int iRet=0; int iTep; for(int i=iFrom;i<iFrom+iLen && i<b.length;i++){ iTep=b[i]; if(iTep<0){ iTep+=256; } //iTep = iTep << (iLen-1-(i-iFrom)*8); //iRet = iRet | iTep; iRet=(iRet<<8)|iTep; } return iRet; } |
|
| 返回顶楼 | |





