/* * Created on Apr 7, 2005 */ package WordPro2Controller; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import javax.swing.JFileChooser; /** Utilities for reading a file. * At present there is only one public method (so that's the one you * might want to look at). * Do not change, since you won't be able to hand in a changed version. */ public class FileUtilities373 { /** Prompt the user to choose a file; locate the file, and read its * entire contents into a string. The file is * assumed to be a text file in the default encoding of the system * (most likely ASCII); files in other formats may be used but * will not be converted correctly to characters. The lines of the * original file are separated by linefeeds within the string. The * full canonical name of the file is printed on the console, along with * an indication of success or failure. * @return a string containing the entire contents of the file, or null * if there was an error. */ public static String chooseFileAndReadIntoString() { JFileChooser chooser = new JFileChooser(); System.out.println("If you don't see a file chooser box, " + "look behind some of the windows."); int choice = chooser.showOpenDialog(null); if (choice == JFileChooser.APPROVE_OPTION) { //user made a choice File chosenFile = chooser.getSelectedFile(); return readFileIntoString(chosenFile); } else { System.out.println("User did not select a file."); return null; } } /** Read a character stream completely into a string. The stream * is not rewound, so any characters already passed are not included * in the output. The lines of the file will be separated by * linefeed characters. * @param openReader an open Reader, which is closed at the end * of the operation. * @return a string with all of the (remaining) characters of the * stream, or null if there was an I/O error processing the stream. */ static String readFileIntoString(Reader openReader) { BufferedReader bReader = new BufferedReader(openReader); StringBuffer wholeFile = new StringBuffer(); String nextLine; try { while ((nextLine = bReader.readLine()) != null) { //got another line if (wholeFile.length() > 0) { //not at beginning of file //add a linefeed to separate from preceding line wholeFile.append("\n"); } wholeFile.append(nextLine); } return wholeFile.toString(); } catch (IOException ex) { return null; } finally { try { openReader.close(); } catch (IOException e) { } } } /** Read the full contents of a file into a string. The file is * assumed to be a text file in the default encoding of the system * (most likely ASCII); files in other formats may be used but * will not be converted correctly to characters. The lines of the * original file are separated by linefeeds within the string. The * full canonical name of the file is printed on the console, along with * an indication of success or failure. * @param path a non-null file name * @return the contents of the file as a string, or null if there was * an IO error processing the file. * @throws IllegalArgumentException if the file does not exist or * cannot be opened for reading. */ static String readFileIntoString(File path) { assert path != null: "path must not be null"; try { String fullpath = path.getCanonicalPath(); if (!path.exists() || ! path.canRead() || path.isDirectory()) { System.out.println( "Can't find or process file " + path + " (" + fullpath + ")"); return null; } //InputStream iStream = new FileInputStream(path); Reader freader = new FileReader(path); String result = readFileIntoString(freader); System.out.println("Opened file " + fullpath + " [" + result.length() + " chars.]"); return result; } catch (IOException e) { System.out.println( "Can't open: error while reading file " + path); return null; } } }