原文地址:https://www.cnblogs.com/baby123/p/12706280.html
如果文件末尾有一串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(); }
原文地址:https://www.cnblogs.com/baby123/p/12706280.html
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/277930.html