001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.samples.ui.texteditor.myplugin.actions;
006    
007    import java.io.*;
008    
009    // this file is taken from: http://www.javapractices.com/Topic42.cjp
010    public class ReadWriteTextFile {
011    
012        /**
013         * Fetch the entire contents of a text file, and return it in a String.
014         * This style of implementation does not throw Exceptions to the caller.
015         *
016         * @param aFile is a file which already exists and can be read.
017         */
018        static public String getContents(File aFile) {
019            //...checks on aFile are elided
020            StringBuffer contents = new StringBuffer();
021    
022            //declared here only to make visible to finally clause
023            BufferedReader input = null;
024            try {
025                //use buffering, reading one line at a time
026                //FileReader always assumes default encoding is OK!
027                input = new BufferedReader(new FileReader(aFile));
028                String line = null; //not declared within while loop
029                /*
030                * readLine is a bit quirky :
031                * it returns the content of a line MINUS the newline.
032                * it returns null only for the END of the stream.
033                * it returns an empty String if two newlines appear in a row.
034                */
035                while ((line = input.readLine()) != null) {
036                    contents.append(line);
037                    contents.append(System.getProperty("line.separator"));
038                }
039            }
040            catch (FileNotFoundException ex) {
041                ex.printStackTrace();
042            }
043            catch (IOException ex) {
044                ex.printStackTrace();
045            }
046            finally {
047                try {
048                    if (input != null) {
049                        //flush and close both "input" and its underlying FileReader
050                        input.close();
051                    }
052                }
053                catch (IOException ex) {
054                    ex.printStackTrace();
055                }
056            }
057            return contents.toString();
058        }
059    
060        /**
061         * Change the contents of text file in its entirety, overwriting any
062         * existing text.
063         * <p/>
064         * This style of implementation throws all exceptions to the caller.
065         *
066         * @param aFile is an existing file which can be written to.
067         * @throws IllegalArgumentException if param does not comply.
068         * @throws FileNotFoundException    if the file does not exist.
069         * @throws IOException              if problem encountered during write.
070         */
071        static public void setContents(File aFile, String aContents)
072                throws FileNotFoundException, IOException {
073            if (aFile == null) {
074                throw new IllegalArgumentException("File should not be null.");
075            }
076            if (!aFile.exists()) {
077                throw new FileNotFoundException("File does not exist: " + aFile);
078            }
079            if (!aFile.isFile()) {
080                throw new IllegalArgumentException("Should not be a directory: " + aFile);
081            }
082            if (!aFile.canWrite()) {
083                throw new IllegalArgumentException("File cannot be written: " + aFile);
084            }
085    
086            //declared here only to make visible to finally clause; generic reference
087            Writer output = null;
088            try {
089                //use buffering
090                //FileWriter always assumes default encoding is OK!
091                aContents = aContents.replaceAll("\n", System.getProperty("line.separator"));
092                output = new BufferedWriter(new FileWriter(aFile));
093                output.write(aContents);
094            }
095            finally {
096                //flush and close both "output" and its underlying FileWriter
097                if (output != null) output.close();
098            }
099        }
100    }