为什么字符串数组空检查不工作



我正在读取一个文本文件并检查空列,但空检查不工作,我得到ArrayIndexOutOfBound异常。

我正在读一个像这样的文本文件

fstream17 = new FileInputStream(
                    Environment.getExternalStorageDirectory() + "/InputFiles"
                            + "/XYZ.TXT");
            in17 = new DataInputStream(fstream17);
            buffreader17 = new BufferedReader(new UnicodeReader(in17));
            while ((strRead = buffreader17.readLine()) != null) {
                splitarray = strRead.split("t");
                Log.d("Split array", ""+splitarray[0]/*+""+splitarray[11]*/);
                if(splitarray[8] == null || splitarray[8].length() == 0) //Checking for null coloumn 
                {
                    Log.d("Null", "Null");
                }

我的UnicodeReader类是这样的

public class UnicodeReader extends Reader
{
    private static final int BOM_MAX_SIZE = 4;
    private InputStreamReader delegate;
    public UnicodeReader(InputStream in) throws IOException {
        init(in, null);
    }
private void init(InputStream in, String defaultEnc) throws IOException {
        String encoding;
        byte bom[] = new byte[BOM_MAX_SIZE];
        int n, unread;
        PushbackInputStream internalIn = new PushbackInputStream(in, BOM_MAX_SIZE);
        n = internalIn.read(bom, 0, bom.length);
        if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
            encoding = "UTF-8";
            unread = n - 3;
        }
        else
            if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
                encoding = "UTF-16BE";
                unread = n - 2;
            }
            else
                if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
                    encoding = "UTF-16LE";
                    unread = n - 2;
                }
                else
                    if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
                        encoding = "UTF-32BE";
                        unread = n - 4;
                    }
                    else
                        if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
                            encoding = "UTF-32LE";
                            unread = n - 4;
                        }
                        else {
                            // Unicode BOM mark not found, unread all bytes
                            encoding = defaultEnc;
                            unread = n;
                        }
        if (unread > 0)
            internalIn.unread(bom, (n - unread), unread);
        else
            if (unread < -1)
                internalIn.unread(bom, 0, 0);
        // Use BOM or default encoding
        if (encoding == null) {
            delegate = new InputStreamReader(internalIn);
        }
        else {
            delegate = new InputStreamReader(internalIn, encoding);
        }
    }

但是这个检查现在不起作用,我现在得到的是这个的ArrayIndexOutOfBound异常

使用说明:

if(splitarray == null || splitarray.length >= 8) //Checking for null coloumn 
{  
  Log.d("Null", "Null"); 
}

或者为了安全起见,当您使用split时,数组的长度是根据您的字符串定义的。因此您不必对元素进行空检查。只要使用.length获取数组的长度,就可以了。

最新更新