BufferedReader in = new BufferedReader(
new FileReader("DisplayProfileXML"));
char[] ch = new char[65535];
StringBuffer sb = new StringBuffer();
String temp = null;
while ( (temp = in.readLine()) != null )
sb.append(temp);System.out.println(sb.toString());
new FileReader("DisplayProfileXML"));
char[] ch = new char[65535];
StringBuffer sb = new StringBuffer();
String temp = null;
while ( (temp = in.readLine()) != null )
sb.append(temp);System.out.println(sb.toString());
2^31 * 16bit = 4GB
通常我们的电脑加上虚拟内存都不会达到4GB,对吧?
(我试过Win2000最多支持5GB物理内存+虚拟内存)所以你的问题出在你的写法:
String是immutable的,每次“+”操作就会复制一个对象。
于是如果你有n个字符,你的写法导致要分配“n!”个字符!(能想明白吗?)
无论什么时候,只要出现str+超过三次,就要考虑是否用StringBuffer来代替
你出错是由于你每次都读65535个字符。
第二次循环时没有这么多个字符,就出错了。
没有这么多个字符是什么意思呀?我一次读65535,我的文件有120K呢,怎么会第二次就没有这么多字符了?To:whyxx(永远成不了高手)
用二进制把整个文件连类型都读进来是什么意思?base64又是怎么回事呀?
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);
BufferedWriter ou = 从数据库中取出来的流。
byte[] xx=new byte[1024];
while(in.read(xx)!=-1){
ou.write(xx);
}
2^31 * 16bit = 4GBhoho,有这么大?这早就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
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
利用一个byte[] buffer = new byte[1024 * 4]作为缓冲
等缓冲满了就清空写入流