I needed to create a class that will handle files and directories for me in order to make the code more clearer and I came up with this:
Expand|Select|Wrap|Line Numbers
- package xmlparser;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- /**
- * This class handle operations on files and directories.
- *
- * @author AYACH MOHAMED
- *
- */
- public class FileDirectoryFactory {
- private String name;
- private File file;
- private FileWriter fstream;
- //private BufferedWriter fileWriter;
- /**
- * This is the constructor of this class
- *
- * @param fstream
- */
- public FileDirectoryFactory() {
- try {
- fstream = new FileWriter(file);
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- /** This function checks if the directory exists
- * if not it creates it.
- *
- * @param fileOrDirectoryName
- * @return directory
- */
- public File createDirectory(String name)
- {
- File directory = new File(name);
- if(!FileOrDirectoryExists(name))
- directory.mkdirs();
- return directory;
- }
- /**
- * This function checks if the file exists:
- * delete and recreates it if it exists
- * if not it creates it.
- *
- * @param fileOrDirectoryName
- * @return
- */
- public FileWriter createFile(String name)
- {
- this.file = new File(name);
- if(!FileOrDirectoryExists(name))
- {
- try{
- file.createNewFile();
- fstream = new FileWriter(file);
- }
- catch (Exception e) {
- System.err.println("Error: can't create\n");
- }
- }
- //erase the content of the file
- else
- {
- try{
- this.file.delete();
- this.file.createNewFile();
- }
- catch (Exception e) {
- System.err.println("Error: can't erease\n");
- }
- }
- return fstream;
- }
- /**
- * This file writes the text into the file.
- *
- * @param text
- */
- public void write(String text)
- {
- try{
- fstream = new FileWriter(file);
- BufferedWriter file = new BufferedWriter(fstream);
- file.write(text);
- file.close();
- }
- catch (Exception e) {
- System.err.println("Error: can't write");
- }
- }
- public void append(String text)
- {
- try{
- fstream = new FileWriter(file, true);
- BufferedWriter file = new BufferedWriter(fstream);
- file.write(text);
- file.close();
- }
- catch (Exception e) {
- System.err.println("Error: can't append");
- }
- }
- /**
- *
- * @param fileOrDirectoryName
- * @return
- */
- public boolean FileOrDirectoryExists(String name)
- {
- File file= new File(name);
- return file.exists();
- }
- }
Expand|Select|Wrap|Line Numbers
- FileDirectoryFactory fdf = new FileDirectoryFactory()
Expand|Select|Wrap|Line Numbers
- public void creatOrmFile(String orm, String ormFileName)//public void creatOrmFile(String orm, String ormFileName)
- {
- if(fdf.FileOrDirectoryExists("META-INF\\" + ormFileName + ".xml"))
- {
- System.out.println("here append");
- fdf.append(orm);
- }
- else
- {
- System.out.println("here write");
- fdf.createFile("META-INF\\" + ormFileName + ".xml");
- fdf.write(ormHead + orm);
- }
- }
Error: can't append
here append
here append
Error: can't append
here append
Error: can't append
is there any better way to do this?