Java 删除 文本文件 最后一行 为 NUL 的字符
作者:互联网
原文地址: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
标签:Java,文本文件,start,NUL,file,isDelete,buf,nextend 来源: https://www.cnblogs.com/chang09/p/16535644.html