本帖最后由 何亚辉 于 2016-6-7 11:50 编辑
package com.heima.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Day1_ClassTest2 {
public static void main(String[] args) throws IOException {
File f = new File("Student.txt");
String name = f.getName();
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[1024 * 1024];
int a;
String s = null;
while ((a = fis.read(b)) != -1) {
s = new String(b, 0, a);
}
s = s.replace("4号:阮清贵"+"\r\n", "");
File f1 = new File("Student2.txt");
FileOutputStream fos = new FileOutputStream(f1);
fos.write(s.getBytes());
fis.close();
fos.close();
System.out.println(f.delete());
File f2 = new File(name);
f1.renameTo(f2);
}
} |