您应该按顺序创建文件阅读器和编写器,而不是一次创建.
FileReader fr = new FileReader(f); FileWriter fw = new FileWriter(f); // here you are deleting your file content before you had chance to read from it
你应该做以下事情:
public static void main(String[] args) throws Exception{ File f = new File("abc.txt"); FileReader fr = new FileReader(f); int count = 0; while(fr.read()!=-1){ count++; } fr.close(); FileWriter fw = new FileWriter(f); while(count-->0){ fw.write('*'); } fw.flush(); fw.close(); }