/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JOptionPane;


public class PlainTextUtilities {

	/** Loads and returns the contents of a text file
	 * 
	 * @param file the file to attempt to load
	 * @return the file contents
	 * @throws FileNotFoundException
	 */
	public static String LoadText(File file) throws FileNotFoundException {
		StringBuffer buffer = new StringBuffer();
		BufferedReader input_text = new BufferedReader( new FileReader(file) );
		
		// Load the text file into the text area
    	try {
    		String line;
    		while(( line = input_text.readLine()) != null) {
    			buffer.append(line);
    			buffer.append("\n");
    		}
    		input_text.close();
    		    
    	} catch (IOException ioe) {
    		System.err.println("Problem loading file: "+file.getName());
    	}
    		
    	if(buffer.length() > 0)
    		return buffer.toString();
    	else
    		return null;
	}
	
	/** Writes specified string data to a file in one pass
	 * 
	 * @param file
	 * @param value
	 * @throws IOException
	 */
	public static void SaveText(File file, String value) throws IOException {
		PrintWriter data;
		data = new PrintWriter(new FileWriter(file));
        data.print(value);
        data.close();
	}
	
}
