473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

FileDirectoryFactory class issues

momotaro
357 100+
Hi,
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
  1. package xmlparser;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6.  
  7. /**
  8.  * This class handle operations on files and directories.
  9.  * 
  10.  * @author AYACH MOHAMED
  11.  *
  12.  */
  13.  
  14. public class FileDirectoryFactory {
  15.  
  16.     private String name;
  17.     private File file;
  18.     private FileWriter fstream;
  19.     //private BufferedWriter fileWriter;
  20.  
  21.     /**
  22.      * This is the constructor of this class
  23.      * 
  24.      * @param fstream
  25.      */
  26.     public FileDirectoryFactory() {
  27.         try {
  28.             fstream = new FileWriter(file);
  29.         } catch (Exception e) {
  30.             // TODO: handle exception
  31.         }
  32.     }
  33.  
  34.     public String getName() {
  35.         return name;
  36.     }
  37.  
  38.     public void setName(String name) {
  39.         this.name = name;
  40.     }
  41.  
  42.     /** This function checks if the directory exists
  43.      * if not it creates it.
  44.      * 
  45.      * @param fileOrDirectoryName
  46.      * @return directory
  47.      */
  48.     public File createDirectory(String name)
  49.     {
  50.         File directory = new File(name);
  51.  
  52.         if(!FileOrDirectoryExists(name)) 
  53.             directory.mkdirs();
  54.         return directory;
  55.     }
  56.     /**
  57.      * This function checks if the file exists:
  58.      * delete and recreates it if it exists 
  59.      * if not it creates it.
  60.      * 
  61.      * @param fileOrDirectoryName
  62.      * @return
  63.      */
  64.     public FileWriter createFile(String name)
  65.     {    
  66.         this.file = new File(name);
  67.         if(!FileOrDirectoryExists(name))
  68.         {
  69.             try{
  70.                 file.createNewFile();
  71.                 fstream = new FileWriter(file);
  72.             }
  73.             catch (Exception e) {
  74.                 System.err.println("Error: can't create\n");
  75.             }
  76.         }
  77.         //erase the content of the file
  78.         else
  79.         {
  80.             try{
  81.                 this.file.delete();
  82.                 this.file.createNewFile();
  83.             }
  84.             catch (Exception e) {
  85.                 System.err.println("Error: can't erease\n");
  86.             }
  87.         }
  88.         return fstream;
  89.     }
  90.  
  91.     /**
  92.      * This file writes the text into the file.
  93.      * 
  94.      * @param text
  95.      */
  96.     public void write(String text)
  97.     {
  98.         try{
  99.             fstream = new FileWriter(file);
  100.             BufferedWriter file = new BufferedWriter(fstream);
  101.             file.write(text);
  102.             file.close();
  103.         }
  104.         catch (Exception e) {
  105.             System.err.println("Error: can't write");
  106.         }
  107.     }
  108.  
  109.  
  110.     public void append(String text)
  111.     {
  112.         try{
  113.             fstream = new FileWriter(file, true);
  114.             BufferedWriter file = new BufferedWriter(fstream);
  115.             file.write(text);
  116.             file.close();
  117.         } 
  118.         catch (Exception e) {
  119.             System.err.println("Error: can't append");
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * 
  125.      * @param fileOrDirectoryName
  126.      * @return
  127.      */
  128.     public boolean FileOrDirectoryExists(String name)
  129.     {
  130.         File file= new File(name);
  131.         return file.exists();
  132.     }
  133. }
  134.  
and I use it like this:
Expand|Select|Wrap|Line Numbers
  1. FileDirectoryFactory fdf = new FileDirectoryFactory()
the thing is when I try this twice I get err messages:
Expand|Select|Wrap|Line Numbers
  1. public void creatOrmFile(String orm, String ormFileName)//public void creatOrmFile(String orm, String ormFileName)
  2.     {
  3.  
  4.  
  5.         if(fdf.FileOrDirectoryExists("META-INF\\" + ormFileName + ".xml"))
  6.         {
  7.             System.out.println("here append");
  8.             fdf.append(orm);
  9.         }
  10.  
  11.         else
  12.         {
  13.             System.out.println("here write");
  14.             fdf.createFile("META-INF\\" + ormFileName + ".xml");
  15.             fdf.write(ormHead + orm);
  16.         }
  17.     }
errors:
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?
Jul 29 '11 #1
1 1233
momotaro
357 100+
I think my fdf object lose its last reference and points to other stuff the that its "somehow" working is that is handling files which get persisted...
Jul 29 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
5
by: MPowell | last post by:
I'm going through the Koeing book Accelerated C++ in an attempt to understand Container classes. Of course I'm going through a paradigm shift from C to C++. So now I've got struct Header {...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
7
by: jsale | last post by:
I have made an ASP.NET web application that connects to SQL Server, reading and writing data using classes. I was recommended to use session objects to store the data per user, because each user...
18
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful...
3
by: Martyn Fewtrell | last post by:
Hi there. First of all let me apologise for this is a somewhat general question to which I would be interested in any opinions. As far as I can see when I create a web application the...
1
by: Jon Ebersole | last post by:
I am developing a webservice and a windows application that talk to each other. They are using a standard VB class library in the background. I am having problems understanding why I can't sync my...
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
6
by: Bradley Plett | last post by:
I have run into this problem occasionally, and have crude ways of getting around it, but I'm wondering if anyone else has a better, more elegant solution. I have a web service and a client...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.