我正在读取一个文本文件并检查空列,但是null检查不起作用,我得到了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异常。
答案 0 :(得分:1)
使用:
if(splitarray == null || splitarray.length >= 8) //Checking for null coloumn
{
Log.d("Null", "Null");
}
或者为了更安全,当你使用split时,数组的长度是根据你的字符串定义的。因此你不必对元素进行空检查。只需使用.length
获取数组的长度,就可以了。