Don't have time tonight to play with this... Here's something to get you started....
*****The Library Class*****
import java.io.*;
class Library {
// you might have some instance variable(s)
public void save(File outfile) throws IOException {
// this should probably save the library!
String tempOut = "";
PrintWriter pwriter = null;
if (outfile == null) {
throw new IllegalArgumentException("File is null.");
}
if (outfile.exists()) {
// prompt to overwrite file
}
if (!outfile.isFile()) {
throw new IllegalArgumentException("File is a directory: " + outfile);
}
if (!outfile.canWrite()) {
throw new IllegalArgumentException("Can not write File: " + outfile);
}
tempOut += "content that you want to print out.";
try {
// Create the file
pwriter = new PrintWriter(new FileWriter(f));
// Write to the file
pwriter.println(tempOut);
// Close the file
pwriter.flush();
pwriter.close();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
Library(File infile) throws FileNotFoundException, IOException {
// this might actually load some data!
if (infile == null) {
throw new IllegalArgumentException("File is null.");
}
if (!infile.exists()) {
throw new FileNotFoundException ("File does not exist: " + infile);
}
if (!infile.isFile()) {
throw new IllegalArgumentException("File is a directory: " + infile);
}
if (!infile.canWrite()) {
throw new IllegalArgumentException("Can not write File: " + infile);
}
BufferedReader input = null;
try
{
//use buffering
//this implementation reads one line at a time
input = new BufferedReader( new FileReader(infile) );
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
// process line
}
input.close();
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
}
public String[] getBooks() {
// this method might return real data somehow!
String[] titles = {"Fix the getBooks method!"};
return titles;
}
public String getLongestBook() {
return "Fix the getLongestBookMethod!";
}
public String getShortestBook() {
return "Fix the getShortestBookMethod!";
}
public String getShortestChapter() {
return "Fix the getShortestChapterMethod!";
}
public String getLongestChapter() {
return "Fix the getLongestChapterMethod!";
}
public String[] getByAuthor(String name, boolean fuzzy) {
String[] titles = {"Fix the getByAuthorMethod!"};
return titles;
}
public int getNumberOfBooks() {
return 1;
}
}
__________________
"Reality is just a crutch for people who can't cope with drugs."
Robin Williams.
|