file writing problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    file writing problems

    I create file then I test and write to it first time everything is good but when I rerun the program it find the file so it should flush its content then do what ever I told it to do but there I get my problem can't erease and can't write:
    Code:
    public FileWriter createFile(String name)
    	{	
    		if(!FileOrDirectoryExists(name))
    		{
    			//erase the content of the file
    			try{
    				fstream = new FileWriter(name);
    			}
    			catch (Exception e) {
    				System.err.println("Error: can't create");
    			}
    		}
    		//erase the content of the file
    		else
    		{
    			try{
    				this.fstream.flush();
    				this.fstream.close();
    			}
    			catch (Exception e) {
    				System.err.println("Error: can't erease");
    			}
    		}
    		return fstream;
    	}
    	
    	/**
    	 * This file writes the text into the file.
    	 * 
    	 * @param text
    	 */
    	public void write(String text)
    	{
    		try{
    			BufferedWriter file = new BufferedWriter(fstream);
    			 file.write(text);
    			 file.close();
    		}
    		catch (Exception e) {
    			System.err.println("Error: can't write");
    		}
    		
    	}
    The calling :
    Code:
    FileDirectoryFactory file = new FileDirectoryFactory();
    		file.createFile("orm.xml");
    		file.write("hello world");
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You close your fstream. Then you return a closed fstream which you don't assign to a variable in your code. Your write function attempts to open an fstream which you don't pass into your code.

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      here is the solution if you think there is any improvement to make tell me
      Code:
      	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");
      		}
      	}

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        FileWriter issues

        I have a class that contains a method "createFile " that returns a FileWriter object then I use that to write into the created file but have a custom message error saying can't write to file, here is the code :
        Code:
        FileWriter classFile = fdf.createFile(dirPath + "\\" 
        + entity.getClassName() + ".java");
        			try {
        				classFile.write(entity.toString());
        				classFile.close();
        			} catch (Exception e) {
        				System.err.println("can't write class [INDENT][INDENT][INDENT][INDENT]to file");[/INDENT][/INDENT][/INDENT][/INDENT]
        			}

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          My bad I wasn't using my method :) thx anyway

          Comment

          Working...