eclipse plugin: check for file existance before using it

add the 'overwrite' flag for overwriting existing files if any
This commit is contained in:
Timotei Dolean 2010-07-04 20:25:50 +00:00
parent 5035b23e1e
commit 821340478b

View file

@ -54,6 +54,9 @@ public class ResourceUtils
public static String getFileContents(File file)
{
if (!file.exists())
return null;
String contentsString = "";
BufferedReader reader = null;
try
@ -135,8 +138,10 @@ public class ResourceUtils
* @param project the project in which the file will be created
* @param fileName the filename of the file
* @param fileContentsString the text which will be contained in the file
* @param overwrite true to overwrite the file if it already exists
*/
public static void createFile(IProject project, String fileName, String fileContentsString)
public static void createFile(IProject project, String fileName, String fileContentsString,
boolean overwrite)
{
IFile file = project.getFile(fileName);
if (fileContentsString == null)
@ -145,6 +150,15 @@ public class ResourceUtils
Logger.print("file contents are null", 2);
}
if (file.exists() && overwrite)
try
{
file.delete(true, null);
} catch (CoreException e)
{
e.printStackTrace();
}
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContentsString.getBytes());
createResource(file, project, fileName, inputStream);
}