/* * HandleFile.java * * Created on January 30, 2006, 11:38 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package redmondfile; import java.io.File; import java.io.*; /** * * @author Mike */ public class HandleFile { /** Creates a new instance of HandleFile */ public HandleFile() { } /** * get started with a file - including choosing it using the FileChooser, * making sure we can read from it */ public static BufferedReader startFile (String filename) throws IOException { ////////////////////////////// // set up for reading file ////////////////////////////// File filehandle = new File(filename); boolean there = filehandle.exists(); FileReader fReader; BufferedReader bReader; String line; if (there) { // if file exists, read the file if (filehandle.canRead() ) { // try to read it fReader = new FileReader(filehandle); bReader = new BufferedReader(fReader); } else { throw new IOException(); // signal that we are unable to handle the file } } else { throw new IOException(); // signal that we are unable to handle the file } return bReader; } /** * call this one the first time when file setup is needed */ public static PrintWriter prepWriteToFile(String filename) throws IOException { File outf = new File(filename); FileWriter outfw = null; try { outfw = new FileWriter(outf); } catch (IOException ioex) { System.out.println("exception - cannot write results" + ioex); throw ioex; // have caller deal with it } BufferedWriter outbw = new BufferedWriter(outfw); PrintWriter outpw = new PrintWriter(outbw); return outpw; } }