java的IO流问题

feng13555 2008-11-04 07:20:36
类A,类B
类A中:URL url = new URL("http://www.baidu.com");
URLConnection connection=url.openConnection();
URLConnection urlcon = url.openConnection();
这里调用类B:
B b = new B();
String code = b.getFlowCode(urlcon.getInputStream());//这里调用了类B的方法返回一个字符串

//代码继续...
//问题:InputStream传进了类B里,如果在使用只能重新url.openconnection();打开连接重新获得流,原因是流在类B里处理过就失效还是什么问题?如何能继续使用这个InputStream()?


类B:public String getFlowCode(InputStream in) {
//代码略
BufferedInputStream imp = new BufferedInputStream(in);
//对IO流进行处理.
}


...全文
142 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
guishuanglin 2008-11-05
  • 打赏
  • 举报
回复
处理流用线程处理:
来了一个请求,生成一个线程,让他自已处理
把连接放入线程:
new Thread(new ProcesConnection(Connection)).start();

cuijie_cn 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 abcniu 的回复:]
mark(int readlimit)
在此输入流中标记当前的位置。
reset()
将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
这都是InputStream里的方法,我们应该可以实现你要的功能,在
开始做一个标记,传到B里后,再调用流的reset就可以了。

感觉上输入流里的东西传到输出流里应该就没有东西了,
但实际上,InputStream的read方法,只是做了数据返回。

public int read(byte b[]…
[/Quote]

```
cuijie_cn 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 abcniu 的回复:]
mark(int readlimit)
在此输入流中标记当前的位置。
reset()
将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
这都是InputStream里的方法,我们应该可以实现你要的功能,在
开始做一个标记,传到B里后,再调用流的reset就可以了。

感觉上输入流里的东西传到输出流里应该就没有东西了,
但实际上,InputStream的read方法,只是做了数据返回。

public int read(byte b[]…
[/Quote]

非典型射手 2008-11-05
  • 打赏
  • 举报
回复
urlcon.getInputStream()只能调用一次.

InputStream is = urlcon.getInputStream

这样使用
gmh521 2008-11-05
  • 打赏
  • 举报
回复
顶![Quote=引用 5 楼 abcniu 的回复:]
mark(int readlimit)
在此输入流中标记当前的位置。
reset()
将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
这都是InputStream里的方法,我们应该可以实现你要的功能,在
开始做一个标记,传到B里后,再调用流的reset就可以了。

感觉上输入流里的东西传到输出流里应该就没有东西了,
但实际上,InputStream的read方法,只是做了数据返回。

public int read(byte b[]…
[/Quote]
macleane 2008-11-05
  • 打赏
  • 举报
回复
同意楼上
pauliuyou 2008-11-05
  • 打赏
  • 举报
回复
bang ding
mjjzg 2008-11-05
  • 打赏
  • 举报
回复
顶一下,以表支持
abcniu 2008-11-04
  • 打赏
  • 举报
回复
mark(int readlimit)
在此输入流中标记当前的位置。
reset()
将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
这都是InputStream里的方法,我们应该可以实现你要的功能,在
开始做一个标记,传到B里后,再调用流的reset就可以了。

感觉上输入流里的东西传到输出流里应该就没有东西了,
但实际上,InputStream的read方法,只是做了数据返回。

public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}

所以流里的东西应该都还在,并没有消失
yiziren 2008-11-04
  • 打赏
  • 举报
回复
while(true){
}
学习了
zhangpeng9886123 2008-11-04
  • 打赏
  • 举报
回复
学习了 友情帮顶一下
英气鬼 2008-11-04
  • 打赏
  • 举报
回复
a类:

public class Aurl {

private static URL url;
private static URLConnection connection;
private static URLConnection urlConnection;

/**
*
* @description
* @author dong_ysheng
* @param args
*/
public static void main(String[] args) {
try {
url = new URL("http://192.168.128.6");
connection = url.openConnection();
urlConnection = url.openConnection();
BUrl bUrl = new BUrl();
System.out.println(bUrl.getFlowCode(urlConnection.getInputStream()));

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


b类:


public class BUrl {

/**
*
*/
public BUrl() {

}

public String getFlowCode(InputStream in) throws IOException{
BufferedInputStream bis = new BufferedInputStream(in);
byte[] buff = new byte[1024];
while (true) {
int indicator = bis.read(buff, 0, 1024);
if (indicator == -1) {
break;
}
System.out.println(new String(buff).trim());
}
String string=new String(buff);
return string;


}

}



我运行怎么没有错误呢?
feng13555 2008-11-04
  • 打赏
  • 举报
回复
顶一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧