Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: File Not Saving

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default File Not Saving

    I'm having a bit of an annoying issue which I cannot figure out.
    My goal is that I have an xml file which I am attempting to save. It is doing this saving via FTP, and the file which I am writing to is on a local VM (with Apache and FileZilla enabled on the VM). I can write other files a similar way, but this one file is not working for some reason.

    Here is a unit test I have to narrow down the issue:
    @Test
    	public void testUpdateConfigurations() {
    		ConfigurationsQuery.readConfigurationsFile();
    		// Increment the simulation date by one month
    		ConfigurationProcess.setMonthData();
     
    		// Get the weather data for the new month
    		Map<WeatherType, WeatherEvent> weatherEvents = WeatherQuery.getWeatherEventsForMonth(Database.getSystemDate().get(Calendar.MONTH));
    		// Clear the old weather data and set the new weather data
    		Database.getWeather().clear();
    		ConfigurationProcess.setWeatherData(weatherEvents);
    		logger.info(SimulationDatabase.getWeatherEventsForMonth().toString());
    		// Set the new simulation dates
    		ConfigurationProcess.setSimDates();
    		Assert.assertTrue(UpdateDatabaseQuery.updateConfigurations());
    	}

    The updateConfigurations() method can be found here:
    public static boolean updateConfigurations() {
    		int count = 0;
    		while(!ConfigurationsQuery.writeConfigurations()) {
    			if(count>=5) {
    				logger.severe("Configurations Failed To Update after 5 attempts...");
    				return false;
    			}
    			count++;
    		}
    		return true;
    	}

    The writeConfigurations() method can be found here:
    public static boolean writeConfigurations() {
    		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    		try {
    			DocumentBuilder builder = factory.newDocumentBuilder();
    			Document document = builder.newDocument();
     
    			// Create Root
    			Element root = document.createElement("config");
    			// I took out all the code which creates the xml file, to keep this short
    			// Append the root
    			document.appendChild(root);
     
    			URL url = new URL(SharedConstants.HOST_URL+FILE_NAME);
    			URLConnection curlconn = url.openConnection();
    			curlconn.setDoOutput(true);
    			logger.info("Do Output True");
     
    			java.io.OutputStream out = curlconn.getOutputStream();
    			if(XMLUtilities.printDocument(document, out)) {
    				out.close();
    				return true;
    			}
    		} catch(Exception ex) {
    			ex.printStackTrace();
    		}		
    		return false;
    	}

    And, lastly, the printDocument() method can be found here:
    public static boolean printDocument(Document doc, OutputStream out) {
    	    TransformerFactory tf = TransformerFactory.newInstance();
    	    Transformer transformer = null;
    		try {
    			transformer = tf.newTransformer();
    		} catch (TransformerConfigurationException e) {
    			e.printStackTrace();
    			return false;
    		}
    	    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    	    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    	    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    	    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    	    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
     
    	    try {
    			transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    			return false;
    		} catch (TransformerException e) {
    			e.printStackTrace();
    			return false;
    		}
    	    return true;
    	}

    I am not getting any exceptions thrown or error messages in the log, and the test passes successfully. The file is just not being updated. Does anyone have any thoughts? I know there is a lot here, so please ask questions if you need something clarified.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: File Not Saving

    EDIT:
    Here is a list of things I've tried so far to debug the issue:
    1. Remove the old file and ran again to see if the file would be created: the test still passed, but the file was not created
    2. Tested against the real server to see if it was a problem with my VM: the test still passed, but the file on the real server was not changed
    3. Added a second transform which prints to the console instead of a file, by changing the tranform's Result parameter to System.out: the xml file is successfully printed to the console correctly, which proves there is no issue with the xml object or the transforming mechanism. Still no change to the file.
    4. Attempted to flush the output stream before closing it: the test still passed, but the file was not changed
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: File Not Saving

    So after banging my head against the wall for like a week over this, I found out what happened. The url I was using was the http address to the file, not the ftp address which I had forgotten that I needed to use, lol. So...this is now resolved, and I feel like an idiot.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Saving new value to file
    By justyStepi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 4th, 2013, 04:02 PM
  2. saving / loading a file
    By Herah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 06:04 AM
  3. Saving to a text file
    By PsYNus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2011, 10:57 AM
  4. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 05:45 AM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM