View Single Post
Old 02-17-2005, 08:39 PM   #4 (permalink)
Chamaeleontidae
aka: freakylongname
 
Chamaeleontidae's Avatar
 
Location: South of the Great While North
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.
Chamaeleontidae is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60