public static void test(String filePath) throws Exception {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);//文件输入流,读取文件
StringBuffer sb = new StringBuffer();//定义缓冲区
FileOutputStream fos = new FileOutputStream("out.txt");//文件输出流
int leng = 0;
byte[] b = new byte[1024];
while ((leng = fis.read(b)) != -1) {//将读取到的文件添加到缓冲区内,并写入文件输出流中
sb.append(new String(b, 0, leng));
fos.write(b);
}
fis.close();//关闭流,释放资源
fos.close();
String txt = sb.toString();//将缓冲区数据变成字符串
for (int i = 0; i < txt.length(); i++) {
String str = txt.substring(txt.length() - 1); // 从后往前 一个一个取字符
String newStr = "";
try {
newStr = txt.replaceAll(str, ""); // 把这个字符都替换掉