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());

解决方案 »

  1.   

    String的大小有限制,这是因为String类实际上使用一个数组来保存所有字符,而数组下标是int类型。所以String的字符个数就不能超过int类型的最大值。但事实上我们一般不会超出这个上限:
    2^31 * 16bit = 4GB
    通常我们的电脑加上虚拟内存都不会达到4GB,对吧?
    (我试过Win2000最多支持5GB物理内存+虚拟内存)所以你的问题出在你的写法:
    String是immutable的,每次“+”操作就会复制一个对象。
    于是如果你有n个字符,你的写法导致要分配“n!”个字符!(能想明白吗?)
      

  2.   

    其实你还不如用二进制把整个文件连类型都读出来,再用base64转成string存到DB里去.
      

  3.   

    所以呢,要用StringBuffer来实现你的要求
    无论什么时候,只要出现str+超过三次,就要考虑是否用StringBuffer来代替
      

  4.   

    base64也太夸张了吧,没必要吧,你又不是中继老式smtp服务器
      

  5.   

    To:li_haizhou(阿土)
    你出错是由于你每次都读65535个字符。
    第二次循环时没有这么多个字符,就出错了。
    没有这么多个字符是什么意思呀?我一次读65535,我的文件有120K呢,怎么会第二次就没有这么多字符了?To:whyxx(永远成不了高手)
    用二进制把整个文件连类型都读进来是什么意思?base64又是怎么回事呀?
      

  6.   

    回:
    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);
      

  7.   

    建议楼主用clob这个数据库属性,这样操作要方便一些。不过二楼的分析确实是有道理的。
      

  8.   

    不建议这样作。应该是一边读一边写,读到多少写多少。BufferedReader in = new BufferedReader(new FileReader("DisplayProfileXML"));
    BufferedWriter ou = 从数据库中取出来的流。
    byte[] xx=new byte[1024];
    while(in.read(xx)!=-1){
       ou.write(xx);
    }
      

  9.   

    String的大小有限制,这是因为String类实际上使用一个数组来保存所有字符,而数组下标是int类型。所以String的字符个数就不能超过int类型的最大值。但事实上我们一般不会超出这个上限:
    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) + "&lt;"+ temp.substring(intCharAt+1,temp.length());
    }
    while ( (intCharAt = temp.indexOf(">")) != -1){
    temp = temp.substring(0, intCharAt) + "&gt;"+ 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
      

  10.   

    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
      

  11.   

    利用缓冲 byte[] buf = new byte[1024];读满了就写
      

  12.   

    同意GJA106(中文字符)的做法
    利用一个byte[] buffer = new byte[1024 * 4]作为缓冲
    等缓冲满了就清空写入流