如何把一个大文件的内容读到一个String里面?

wkgenius 2005-01-20 08:07:08
我要往数据库里面写一个值,这个值的内容是一个文件的内容,这个文件很大,有120k,所以,就要先将这个文件的内容读到一个String里面,然后写到数据库里面
我这样
BufferedReader in = new BufferedReader(
new FileReader("DisplayProfileXML"));
char[] ch = new char[65535];
String str = new String();
int nRead = 0;
while(true){
int n = in.read(ch, nRead, 65535);
nRead += n;
String str1 = new String(ch);
str += str1;
}

System.out.println(str);

现在的问题是第二次调用read的时候就会报Index溢出的异常
java.lang.IndexOutOfBoundsException
at java.io.BufferedReader.read(BufferedReader.java:256)
at Test.main(Test.java:86)

请问该怎么办?
除了read时候的索引,String的大小有限制吗?
...全文
313 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbn9435 2005-01-22
  • 打赏
  • 举报
回复
同意GJA106(中文字符)的做法
利用一个byte[] buffer = new byte[1024 * 4]作为缓冲
等缓冲满了就清空写入流
wkgenius 2005-01-21
  • 打赏
  • 举报
回复
To:li_haizhou(阿土)
你出错是由于你每次都读65535个字符。
第二次循环时没有这么多个字符,就出错了。
没有这么多个字符是什么意思呀?我一次读65535,我的文件有120K呢,怎么会第二次就没有这么多字符了?

To:whyxx(永远成不了高手)
用二进制把整个文件连类型都读进来是什么意思?base64又是怎么回事呀?
drugon 2005-01-21
  • 打赏
  • 举报
回复
建议楼主用clob这个数据库属性,这样操作要方便一些。不过二楼的分析确实是有道理的。
cutelion 2005-01-21
  • 打赏
  • 举报
回复
本贴有收葳价值。 UP
zedk928 2005-01-21
  • 打赏
  • 举报
回复
学习
ztchi 2005-01-21
  • 打赏
  • 举报
回复
学习
stephen129 2005-01-21
  • 打赏
  • 举报
回复
受益,MARK
BasaraTracy 2005-01-21
  • 打赏
  • 举报
回复
有道理
cfsego 2005-01-21
  • 打赏
  • 举报
回复
回:
String类型的使用方式就好象CD-R
一次写入之后就不能修改,只能重新赋值
你的str+=str1;
不论用法正确与否,风格都是不足取的,典型的c++写法
一般的,对String的用法:
String xx = "yy";
xx = "zzz"; //可行
遇到要实现xx和yy的合并,用xx=xx+yy不好
碰到这种情况最好还是用StringBuffer
StringBuffer xx = new StringBuffer("yy");
xx.append(yy); // 或者 xx.insert(0,yy);
zealVampire 2005-01-21
  • 打赏
  • 举报
回复
利用缓冲 byte[] buf = new byte[1024];读满了就写
panbenxi 2005-01-21
  • 打赏
  • 举报
回复
byte[] data = null;
try {
FileInputStream fi = new FileInputStream("filename);
fileLength = fi.available();
data = new byte[fileLength];
fi.read(data, 0, fileLength);
}
catch (Exception ie) {
System.out.println("error:" + ie);
}
在byte数组转换成为String
reene2008 2005-01-21
  • 打赏
  • 举报
回复
学习
eureka0891 2005-01-21
  • 打赏
  • 举报
回复
String的大小有限制,这是因为String类实际上使用一个数组来保存所有字符,而数组下标是int类型。所以String的字符个数就不能超过int类型的最大值。

但事实上我们一般不会超出这个上限:
2^31 * 16bit = 4GB

hoho,有这么大?这早就OutOfMemmoryError了,
不过好像在5M以后的String是没问题的(呵呵,我就做过)
可以用这个类:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Created on 2004/12/17
*
* To change the template for this generated file go to Window - Preferences -
* Java - Code Generation - Code and Comments
*/
/**
* @author Administrator
*
* To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class FileIo {
private static int BLKSIZ = 1024;

int x = 16;
{
int x = 24;
}

public static String readerToString(String strFileName) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader is = new BufferedReader(new InputStreamReader(
new FileInputStream(strFileName),"GB2312") );
try {
String temp = null;
int intCharAt;
while ((temp = is.readLine()) != null) {
while ( (intCharAt = temp.indexOf("<")) != -1){
temp = temp.substring(0, intCharAt) + "<"+ temp.substring(intCharAt+1,temp.length());
}
while ( (intCharAt = temp.indexOf(">")) != -1){
temp = temp.substring(0, intCharAt) + ">"+ temp.substring(intCharAt+1,temp.length());
}
sb.append(temp);
sb.append("\r\n");
}



} finally {
is.close();
}
return sb.toString();
}
public static void main(String[] args) {
try{
System.out.println(readerToString(new String("d:\\abc.txt")));
}catch(IOException e){
e.printStackTrace();
}
}
}
源于JavaCook book
mindynasty 2005-01-21
  • 打赏
  • 举报
回复
我举
GJA106 2005-01-21
  • 打赏
  • 举报
回复
不建议这样作。应该是一边读一边写,读到多少写多少。

BufferedReader in = new BufferedReader(new FileReader("DisplayProfileXML"));
BufferedWriter ou = 从数据库中取出来的流。
byte[] xx=new byte[1024];
while(in.read(xx)!=-1){
ou.write(xx);
}
sqlink 2005-01-20
  • 打赏
  • 举报
回复
base64也太夸张了吧,没必要吧,你又不是中继老式smtp服务器
wjsfr 2005-01-20
  • 打赏
  • 举报
回复
所以呢,要用StringBuffer来实现你的要求
无论什么时候,只要出现str+超过三次,就要考虑是否用StringBuffer来代替
boy 2005-01-20
  • 打赏
  • 举报
回复
有道理
samkuang 2005-01-20
  • 打赏
  • 举报
回复
学习
whyxx 2005-01-20
  • 打赏
  • 举报
回复
其实你还不如用二进制把整个文件连类型都读出来,再用base64转成string存到DB里去.
加载更多回复(4)

62,614

社区成员

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

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