- As seen in class, you can read and write files using a MyReader and a MyWriter. They are
here, and here.
- You should create new files in NetBeans with those names and copy their contents into the classes in your project (be sure
they are in the same package as your editor!).
- To implement save without forcing the user to renavigate the path to the directory they opened the file
in, you must save the pathname they navigated to to open the file. To do that send the getPath() message
to the MyReader (*after* they have selected the file!), and store it in a String variable for later use.
Like this:
MyReader mr;
mr = new MyReader();
currentPath = mr.getPath();
- Now when they save the file you can create a MyWriter pointed to it by:
MyWriter mw = new MyWriter(currentPath);
mw.println(theTA.getText());
mw.close();
- If the user creates a new file, it will not have a path, so, when they save it you should create
the MyWriter like this:
MyWriter mw = new MyWriter();
instead.
- To know which of the above to do, you will need a variable. Like this:
boolean needPath = true;
...
void save() {
MyWriter mw;
if (needPath) {
mw = new MyWriter();
needPath = false;
currentPath = mw.getPath();
} else {
mw = new MyWriter(currentPath);
}
mw.println(theTA.getText());
mw.close();
}
and, when they open a file, set needPath = false, and when they make a new
file, set needPath = true;.
- When they say New File, try new EditorFrame()