您可以使用RandomAccessFile类查找文件的末尾,读取它,然后使用截断该文件
setLength()。
更新: 这是一些代码:
File target = new File("/path/to/file");RandomAccessFile file = new RandomAccessFile(target,"rwd");file.seek(target.length()-1); // Set the pointer to the end of the filechar c = file.readChar();if(c == '|') // Change the pipe character to whatever your sentinel character is{ file.setLength(target.length()-1); // Strip off the last _byte_, not the last character}注意:我尚未测试此代码,没有错误处理等。
如果文件采用任何非8位字符集,
target.length()-1则需要对其进行调整。



