private String readFile(String filePath, BufferedReader reader)
    throws FileNotFoundException, IOException {
        codeString = null;
        int i = 0;
        FileInputStream fis = new FileInputStream(filePath);
        if (reader == null)
        reader = new BufferedReader(new InputStreamReader(fis));
        String line = reader.readLine();
        while (line != null){
            line = line + reader.readLine();}
        return line;
    }

解决方案 »

  1.   

    最好不要用readLine(),readLine会把读取的换行符全部丢弃掉,读出来的文本就不完整了。
      

  2.   

    我也给你一个方法,你自己选择吧:
    static public String readtoEnd(Reader reader) throws IOException {
    final int totalLength = 1024;
    char[] recvChars = new char[totalLength];
    int readLength = reader.read(recvChars);
    String strResult = "";
    while (readLength>0){
    strResult = strResult.concat(new String(recvChars).substring(0,readLength));
    if(readLength < totalLength)try{Thread.sleep(20);}catch(InterruptedException e){}
    readLength = reader.read(recvChars);
    }
    return strResult;
    }
    这是个比较通用的读流的全部数据的方法,可以读文件流、网络流等。
      

  3.   

    我的一个方法:
      public String getText(String filePath) {
        File textFile = new File(parentPath+filePath);
        byte[] bf = new byte[1024];
        int read;
        StringBuffer textContent = new StringBuffer();
        FileInputStream from = null;
        try {
          from = new FileInputStream(textFile);
          while ( (read = from.read(bf)) != -1) {
            textContent.append(new String(bf));
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        finally {
          try {
            from.close();
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
        String result = new String(textContent);
        result = result.trim();
        return result;
      }
      

  4.   

    1024实际上仍然很耗资源,我只好选择readline,再做加'/n'的处理了
      

  5.   

    public void getFileContent(){ 
    try { 
    //读文件 
    InputStream is = new FileInputStream("输入文件路径/文件名"); 
    InputStreamReader isr = new InputStreamReader(is); 
    String s = null; 
    int c = 0; 
    StringBuffer sb = new StringBuffer(); 
    char b[] = new char[1]; 
    try { 
    while((c = isr.read(b))>0){ 
    sb.append(b,0,c); 


    catch (IOException ex) { 

    //写文件 
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("输出文件路径/文件名"),"UTF-8"); 
    osw.write(sb.toString()); 

    catch (Exception ex) { 
    ex.printStackTrace(); 

    }
      

  6.   

    用readline,然后添加换行府,这样比较快速。但是如果要控制读入的字符串大小的话最好还是用bit流比较好点,呵呵