你为什么要读入String啊!String对象的容量是有限的!如果读入大的数据,会造成溢出!最好的办法就是利用StringBuffer来读取!可以动态的根据读入的文件的大小申请空间!

解决方案 »

  1.   

    我主要是为了好处理文字内容,放在String还是StringBuffer中无所谓。我也知道要用到输入流的知识,我就是想知道如何将其中文字信息包装到一个String或是StringBuffer中,具体怎么做呀,里面字符不能有丢失,速度效率要高。请帮忙解答呀。会的帮帮忙呀
      

  2.   

    BufferedReader reader = new BufferedReader(new FileReader("x.txt"));
    StringBuffer buffer;
    String line;
    while ((line = reader.readLine()) != null) {
        buffer.append(line);
        buffer.append("\n");
    }
    reader.close();
    String whichYouWant = buffer.toString();
      

  3.   

    FileInputStream inFile = null;

    try
    {
    inFile= new FileInputStream (file);
    }catch(FileNotFoundException ex){
    ex.printStackTrace();
    }

    FileChannel inChannel = inFile.getChannel();

    ByteBuffer buf =null;

    try{
    buf=ByteBuffer.allocate((int)inChannel.size());
    }
    catch(IOException ex2){
    ex2.printStackTrace();
    }

    String str=null;

    try
    {
    while(inChannel.read(buf)!=-1)
    {
    str=new String(buf.array());
    buf.clear();
    }

    inFile.close();
    }
    catch(IOException ex1)
    {
    ex1.printStackTrace();
    }