从阅读器中删除或忽略字符 [英] Remove or ignore character from reader

查看:110
本文介绍了从阅读器中删除或忽略字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将所有字符读入流中。我正在使用inputStream.read读取它。这是java.io.Reader inputStream。
如何在读入缓冲区时忽略@等特殊字符。

i am reading all characters into stream. I am reading it with inputStream.read. This is java.io.Reader inputStream. How can i ignore special characters like @ when reading into buffer.

代码

private final void FillBuff() throws java.io.IOException
  {
     int i;
     if (maxNextCharInd == 4096)
        maxNextCharInd = nextCharInd = 0;

     try {
        if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
                                            4096 - maxNextCharInd)) == -1)
        {
           inputStream.close();
           throw new java.io.IOException();
        }
        else
           maxNextCharInd += i;
        return;
     }
     catch(java.io.IOException e) {
        if (bufpos != 0)
        {
           --bufpos;
           backup(0);
        }
        else
        {
           bufline[bufpos] = line;
           bufcolumn[bufpos] = column;
        }
        throw e;
     }
  }


推荐答案

你可以使用自定义 FilterReader

class YourFilterReader extends FilterReader{
    @Override
    public int read() throws IOException{
        int read;
        do{
            read = super.read();
        } while(read == '@');

        return read; 
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException{
        int read = super.read(cbuf, off, len);

        if (read == -1) {
            return -1;
        }

        int pos = off - 1;
        for (int readPos = off; readPos < off + read; readPos++) {
            if (read == '@') {
                continue;
            } else {
                pos++;
            }

            if (pos < readPos) {
                cbuf[pos] = cbuf[readPos];
            }
        }
        return pos - off + 1;
    }
}






资源:

  • Javadoc - FilterReader
  • BCRDF - Skipping Invalid XML Character with ReaderFilter

关于同一主题:

  • filter/remove invalid xml characters from stream

这篇关于从阅读器中删除或忽略字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆