求教:String对象作为输入流,相应的写到文件中的流操作

sdut_akaliusi202 2009-12-10 08:08:52
求教:-----struts的--- 动态多文件上传,前台通过脚本获得文件内容,动态添加<input type="hidden" name="fileContent">标签。将文件内容赋到value中。 该页面form对应的formbean中定义了一个string类型的数组String[] fileContent 用来存取前台jsp页面 input标签 传过来的值。
该页面对应得Action.java 中,通过String[] fileCnt= batchform.getFileContent(); 获取从FormBean中取出传过来的文件内容(string类型的)。也就是说现在的 数组fileCnt 中存放的是各个文件的文件内容。 下面问题来了。流读写操作,小弟不知道如何解决。目前做法是
BufferedReader in =new BufferedReader(new StringReader(fileCnt[i])); 形成流
PrintWriter out =new PrintWriter(new BufferedWriter(new FileWriter(dir+"/"+fileNm[i]))); 写流到文件
int lineCount = 1;
while((s = in.readLine()) != null)
out.println(lineCount++ + ":" + s);
out.close();
in.close();
可是当文件内容大的情况下,接近1M左右时,就是不好使。也就是说写流到文件这一块有问题。请教各位老师给帮忙解决一下。非常感谢。 QQ 315780342
...全文
244 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdut_akaliusi202 2009-12-11
  • 打赏
  • 举报
回复

呵呵,谢谢惠顾
[Quote=引用 6 楼 mawentao728 的回复:]
一楼朋友的头像太刺眼了!
[/Quote]
sdut_akaliusi202 2009-12-11
  • 打赏
  • 举报
回复

呵呵,谢谢
[Quote=引用 3 楼 zl3450341 的回复:]
参考下这个
http://hi.baidu.com/flymz/blog/item/c0fa35fa99b721899f514613.html
[/Quote]
Trinx 2009-12-11
  • 打赏
  • 举报
回复
一楼朋友的头像太刺眼了!
sdut_akaliusi202 2009-12-11
  • 打赏
  • 举报
回复
这个方法果然好使,谢谢

[Quote=引用 2 楼 crazylaa 的回复:]
BufferedReader br = new BufferedReader(new StringReader(fileCnt[i]));
FileWriter fw = new FileWriter(dir+"/"+fileNm[i]);
char[] chars= new char[2048];
int s = 0;
int success = 0;
while ((s = br.read(chars)) != -1) {
fw.write(chars, 0, s);
success++;
}
if (success == 0) {
throw new Exception("错误!");
}
[/Quote]
sdut_akaliusi202 2009-12-11
  • 打赏
  • 举报
回复
恩。多谢。呵呵,果然好使
crazylaa 2009-12-10
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new StringReader(fileCnt[i]));
FileWriter fw = new FileWriter(dir+"/"+fileNm[i]);
char[] chars= new char[2048];
int s = 0;
int success = 0;
while ((s = br.read(chars)) != -1) {
fw.write(chars, 0, s);
success++;
}
if (success == 0) {
throw new Exception("错误!");
}
道光2008 2009-12-10
  • 打赏
  • 举报
回复
不要一次性读取,缓冲下,
参考:

buffer = new byte[1024];
String path = request.getSession().getServletContext().getRealPath(
"/images/nophoto.gif");
File file = new File(path);
try {
is = new FileInputStream(file);
if (is.available() > 1024) {
int remain;
while ((remain = is.read()) != -1) {
out.write(remain);
}
} else {
is.read(buffer);
out.write(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}


67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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