/*
Copyright (c) 2009 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the above
copyright notice and the following two paragraphs appear in all copies
of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OFCALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
*/
package org.clothocore.util.basic;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.openide.util.Exceptions;
/**
*
* @author sixpi
*/
public class FileUtils {
/**
*
* @param fileName
* @return
*/
public static boolean isXMLFile(String fileName) {
return isFileType(fileName, "xml");
}
/**
*
* @param fileName
* @param extension without the .
* @return
*/
public static boolean isFileType(String fileName, String extension) {
return fileName.endsWith( "." + extension );
}
public static String readFile(String filePath) {
BufferedReader reader = null;
File file = new File(filePath);
if(!file.exists()) {
return "";
} else {
try {
StringBuffer contents = new StringBuffer();
reader = new BufferedReader(new FileReader(file));
String text = null;
//Read the file into the StringBuffer
while ((text = reader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
//Close the file and output the String
reader.close();
String outstring = contents.toString();
return outstring;
} catch ( FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
return "";
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
return "";
}
}
}
public static void writeFile(String datafile, String filePath) {
try {
Writer output = null;
File file = new File(filePath);
output = new BufferedWriter(new FileWriter(file));
output.write(datafile);
output.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}