请教输入流的问题

随风来去 2003-11-14 10:24:57
在一个方法里
我想读取一个文件的输入流
然后转化为一个对象输入流
同时将另一个对象加到这个流中
然后返回这个新的输入流

能这样做没?该怎么做?
谢谢
...全文
34 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
kunbone 2003-11-19
  • 打赏
  • 举报
回复
UP
shipp 2003-11-18
  • 打赏
  • 举报
回复
UP
xiachedan 2003-11-14
  • 打赏
  • 举报
回复
关注
随风来去 2003-11-14
  • 打赏
  • 举报
回复
问题就是如何将别的输入流一个一个加入BufferedInputStream?
leier1979 2003-11-14
  • 打赏
  • 举报
回复
用BufferedInputStream和BufferedOutputStream来实现,一个一个往里写,应该可以吧
随风来去 2003-11-14
  • 打赏
  • 举报
回复
而且这个流需要读多次?
能不能变成一个单一的流?
随风来去 2003-11-14
  • 打赏
  • 举报
回复
这样可以连接两个流,但是如果将一个对象转为输入流呢?
yesj 2003-11-14
  • 打赏
  • 举报
回复
用SequenceInputStream
随风来去 2003-11-14
  • 打赏
  • 举报
回复
这样清楚了?
public InputStream getStream()
{
File file = new File ("temp.txt");
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
String a = “new String”;
.............

return (ObjectInputSteam)inputStream; //该输入流包含了oin和a的内容

}
hxzhappy 2003-11-14
  • 打赏
  • 举报
回复
还是没有明白您的意思
请戏说!!!
LoveRose 2003-11-14
  • 打赏
  • 举报
回复
你的意思是,用这个对象流再创建一个对象流
随风来去 2003-11-14
  • 打赏
  • 举报
回复
是不是要使用pipe来转化一下?
先都写到管道output,然后再把pipedInput返回
随风来去 2003-11-14
  • 打赏
  • 举报
回复
就是返回的是一个流
fin 和oin 都需要返回 一个 in
怎么做?
ddbean 2003-11-14
  • 打赏
  • 举报
回复
关注!
LoveRose 2003-11-14
  • 打赏
  • 举报
回复
File file = new File ("temp.txt");
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
不知道你说的 “同时将另一个对象加到这个流中“什么意思?

随风来去 2003-11-14
  • 打赏
  • 举报
回复
这样就可以了 ,谢谢
cbhyk 2003-11-14
  • 打赏
  • 举报
回复
是有问题,这样就可以了:
public InputStream getStream()
{
File file = new File ("temp.txt");
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
String a = “new String”;

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(a);
try
{
while(true)
oos.writeObject(oin.readObject());
}
catch(EOFException e)
{
}
oos.flush();
oin.close();
ObjectInputStream result = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
return result;
}
随风来去 2003-11-14
  • 打赏
  • 举报
回复
faint
那可以
ObjectInputStream ois = new ObjectInputStream(new StringBufferInputStream(a));

不知道行不行?

cbhyk 2003-11-14
  • 打赏
  • 举报
回复
return new ObjectInputStream(new SequenceInputStream(ois, oin));
cbhyk 2003-11-14
  • 打赏
  • 举报
回复
是不是这样:
public InputStream getStream()
{
File file = new File ("temp.txt");
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
String a = “new String”;

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writObject(a);
byte[] objData = bos.toByteArray();

ByteArrayInputStream bis = new ByteArrayInputStream(objData);
ObjectInputStream ois = new ObjectIpuStream(bis);

return new SequenceInputStream(ois, oin);
}
加载更多回复(2)

62,614

社区成员

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

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