有段代码,如果文件末尾有一串NUL,就会报错
因此想去掉NUL,再使用工具解析
NUL即ascii码值为0的比特
public static void delNulAtLastLine(String fileName) throws Exception { RandomAccessFile file = new RandomAccessFile(fileName, "rw"); long len = file.length(); long start = file.getFilePointer(); long nextend = start + len - 1; int i = -1; // 移动指针 file.seek(nextend); byte[] buf = new byte[1]; boolean isDelete = true; while (nextend > start) { i = file.read(buf, 0, 1); if(buf[0]==0){ isDelete = true; } if (buf[0] == ' ') { // 删除最后一行 if(isDelete){ file.setLength(nextend - start); } break; } nextend--; file.seek(nextend); } file.close(); }