本人用StringReader读取一个字符串,字符串长度为1100000以上,是一段标签。结果读取后长度小于原字符串长度(后面的部分被截掉了),请各位大侠支支招!小男子无以为报,愿以分相许!哈哈!
注:可以考虑用其它方法,但必须将这个字符串放到一个Reader(如FileReader)里。

解决方案 »

  1.   

    StringReader用来读取的默认空间不够了?
      

  2.   

    楼主,那要看你的目的是什么了。
    是否是读取这个标签再写到另一个文件中,还是读取这个标签使用。
    第一种字节流可以,第二种BufferReader的readLine应该可以。
      

  3.   


    看如下实例:public static void main(String[] args) throws IOException {
            FileWriter fileWriter = new FileWriter(new File("c:/test.txt"));
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for (int i = 0; i < 10000000; i++) {
                bufferedWriter.write("12345678901234567890");
            }
            bufferedWriter.flush();
            bufferedWriter.close();
            FileReader fileReader = new FileReader(new File("c:/test.txt"));
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            bufferedReader.skip(1000000l); //跳过长度-字节
            char[] bs = new char[1];
            bufferedReader.read(bs);
            System.out.println(bs[0]);
        }API:http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/io/BufferedReader.html
      

  4.   

    感谢各位的热心帮助,但还是没有找到思路,还是把代码贴一下吧:
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{      request.setCharacterEncoding("utf-8");//注意编码      String type = request.getParameter("type");      String svg = request.getParameter("svg");//就是要读取这个字符串,稍短时没有问题      response.setCharacterEncoding("utf-8");      ServletOutputStream out = response.getOutputStream();      if (null != type && null != svg){      svg = svg.replaceAll(":rect", "rect");       String ext = "";      Transcoder t = null;     if (type.equals("image/png")) {         ext = "png";         t = new PNGTranscoder();           } else if (type.equals("image/jpeg")) {         ext = "jpg";          t = new JPEGTranscoder();      } else if (type.equals("application/pdf")) {         ext = "pdf";         t = new PDFTranscoder();      } else if (type.equals("image/svg+xml")) {         ext = "svg";         }        response.addHeader("Content-Disposition", "attachment; filename=chart."+ext);      response.addHeader("Content-Type", type);       if (null != t){            StringReader sr = new StringReader(svg); //问题出在这儿,由于svg太长而被截取
                TranscoderInput input = new TranscoderInput(sr);            TranscoderOutput output = new TranscoderOutput(out);            try {               t.transcode(input,output);//在此处抛出了异常
                } catch (TranscoderException e){               out.print("编码流错误.");               e.printStackTrace();            }           } else if (ext == "svg"){                     svg =  svg.replace("http://www.w3.org/2000/svg", "http://www.w3.org/TR/SVG11/");            out.print(svg);         } else {            out.print("Invalid type: " + type);         }      } else {         response.addHeader("Content-Type", "text/html");      }      out.flush();      out.close();      } }
      

  5.   


    Transcoder svg的处理。。没有做过。。不能确定能不能分段处理。。不过LZ可以试试:
    String svg = request.getParameter("svg");  
    传过来的svg参数,在传递的时候可以检查固定长度。。然后分段传递一个数组类型的过来,,程序接收数组类型的多段字符串片段StringReader sr = new StringReader(svg); 
    TranscoderInput input = new TranscoderInput(sr); 
    这里在循环将每个片段包装。。
    t.transcode(input,output);  // 这里看能否将所有片段组合进去。。
      

  6.   

    StringReader sr = new StringReader(svg); //问题出在这儿,由于svg太长而被截取
    想办法确认一下这里被截取是不是因为jvm内存不足引起,比如用java.lang.Runtime类的一些方法
      

  7.   

    肯定不是jvm问题,svg打印出来是完整的,但用StringReader读取时被截断了
      

  8.   

    估计还是你转换类内部读取的问题,StringReader代码很简单,出错机会都没有/*
     * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */package java.io;
    /**
     * A character stream whose source is a string.
     *
     * @author      Mark Reinhold
     * @since       JDK1.1
     */public class StringReader extends Reader {    private String str;
        private int length;
        private int next = 0;
        private int  = 0;    /**
         * Creates a new string reader.
         *
         * @param s  String providing the character stream.
         */
        public StringReader(String s) {
            this.str = s;
            this.length = s.length();
        }    /** Check to make sure that the stream has not been closed */
        private void ensureOpen() throws IOException {
            if (str == null)
                throw new IOException("Stream closed");
        }    /**
         * Reads a single character.
         *
         * @return     The character read, or -1 if the end of the stream has been
         *             reached
         *
         * @exception  IOException  If an I/O error occurs
         */
        public int read() throws IOException {
            synchronized (lock) {
                ensureOpen();
                if (next >= length)
                    return -1;
                return str.charAt(next++);
            }
        }    /**
         * Reads characters into a portion of an array.
         *
         * @param      cbuf  Destination buffer
         * @param      off   Offset at which to start writing characters
         * @param      len   Maximum number of characters to read
         *
         * @return     The number of characters read, or -1 if the end of the
         *             stream has been reached
         *
         * @exception  IOException  If an I/O error occurs
         */
        public int read(char cbuf[], int off, int len) throws IOException {
            synchronized (lock) {
                ensureOpen();
                if ((off < 0) || (off > cbuf.length) || (len < 0) ||
                    ((off + len) > cbuf.length) || ((off + len) < 0)) {
                    throw new IndexOutOfBoundsException();
                } else if (len == 0) {
                    return 0;
                }
                if (next >= length)
                    return -1;
                int n = Math.min(length - next, len);
                str.getChars(next, next + n, cbuf, off);
                next += n;
                return n;
            }
        }    /**
         * Skips the specified number of characters in the stream. Returns
         * the number of characters that were skipped.
         *
         * <p>The <code>ns</code> parameter may be negative, even though the
         * <code>skip</code> method of the {@link Reader} superclass throws
         * an exception in this case. Negative values of <code>ns</code> cause the
         * stream to skip backwards. Negative return values indicate a skip
         * backwards. It is not possible to skip backwards past the beginning of
         * the string.
         *
         * <p>If the entire string has been read or skipped, then this method has
         * no effect and always returns 0.
         *
         * @exception  IOException  If an I/O error occurs
         */
        public long skip(long ns) throws IOException {
            synchronized (lock) {
                ensureOpen();
                if (next >= length)
                    return 0;
                // Bound skip by beginning and end of the source
                long n = Math.min(length - next, ns);
                n = Math.max(-next, n);
                next += n;
                return n;
            }
        }    /**
         * Tells whether this stream is ready to be read.
         *
         * @return True if the next read() is guaranteed not to block for input
         *
         * @exception  IOException  If the stream is closed
         */
        public boolean ready() throws IOException {
            synchronized (lock) {
            ensureOpen();
            return true;
            }
        }    /**
         * Tells whether this stream supports the () operation, which it does.
         */
        public boolean Supported() {
            return true;
        }    /**
         * Marks the present position in the stream.  Subsequent calls to reset()
         * will reposition the stream to this point.
         *
         * @param  readAheadLimit  Limit on the number of characters that may be
         *                         read while still preserving the .  Because
         *                         the stream's input comes from a string, there
         *                         is no actual limit, so this argument must not
         *                         be negative, but is otherwise ignored.
         *
         * @exception  IllegalArgumentException  If readAheadLimit is < 0
         * @exception  IOException  If an I/O error occurs
         */
        public void (int readAheadLimit) throws IOException {
            if (readAheadLimit < 0){
                throw new IllegalArgumentException("Read-ahead limit < 0");
            }
            synchronized (lock) {
                ensureOpen();
                 = next;
            }
        }    /**
         * Resets the stream to the most recent , or to the beginning of the
         * string if it has never been ed.
         *
         * @exception  IOException  If an I/O error occurs
         */
        public void reset() throws IOException {
            synchronized (lock) {
                ensureOpen();
                next = ;
            }
        }    /**
         * Closes the stream and releases any system resources associated with
         * it. Once the stream has been closed, further read(),
         * ready(), (), or reset() invocations will throw an IOException.
         * Closing a previously closed stream has no effect.
         */
        public void close() {
            str = null;
        }
    }
      

  9.   

    仅凭这个能说明什么呢,能说明到svg完整打印内存是足够的
    这里并没有排除内存不足的可能性
      

  10.   

    1.StringReader sr = new StringReader(svg)
    这行代码之前打印svg.length()确认svg长度,
    之后使用int i=0;
    while (sr.read()!=-1) {
    i++;
    }打印i确认sr中包含字符串的长度。
    2.如果确认以上两个长度不相等,那就是StringReader类的bug了……这种java已建立多年的类,这样的概率有多大?经本人测试,jvm的Xmx=1024m下,字符串长度1000000000,StringReader并不会截断。
    3.如果相等,那要么是jvm内存问题,可调整jvm尝试,要么是楼主自己的类的内部转换问题。
      

  11.   

    建议把t.transcode(input,output)的内部代码贴出来,严重怀疑是这里面的问题……
    1100000长度不大可能内存不足,即使jvm内存不足,也不会截断,直接溢出了