I went through a java tutorial that allowed me to create a text file and write the words,"20 Bruce Wayne" in it. The last method that is called in the main class is named closeFile() that "closes" the text file after it is created.
Why does the file need to be "closed" if I didn't really open it? By "open", I mean the Notepad editor(not the IDE I'm using) pops up with the words "20 Bruce Wayne". Please answer my question in layman's terms.
Main.java:
class apple {
public static void main(String[] args)
{
createfile g = new createfile();
g.openFile();
g.addRecords();
g.closeFile();
}
}
createfile.java
public class createfile {
private Formatter x;
public void openFile(){
try{
x = new Formatter("supermanvsbatman.txt");
}
catch(Exception e){
System.out.println("you have an error");
}
}
public void addRecords(){
x.format("%s%s%s","20 ", "Bruce ", "Wayne ");
}
public void closeFile(){
x.close();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…