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 9 of 9

Thread: .propertie files...

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default .propertie files...

    I created 2 methods 1 for loading string from .propertie file and 1 for savng string to propertie file:

    	public void SavePropertie(String file, String key, String value){
    		Properties properties = new Properties();
    		File xfile = new File("plugins/UnixRPG/" + file + ".properties"); 
    		if (xfile.exists()){
    			//Copying the old part.
    			try {		
    				FileInputStream in = new FileInputStream("plugins/UnixRPG/" + file + ".properties");
    				properties.load(in);
    				in.close();
    			} catch (IOException e) {}
    			@SuppressWarnings("rawtypes")
    			Enumeration em = properties.keys();
    			while(em.hasMoreElements()){
    				String str = (String) em.nextElement();
    				if (str != key){
    					properties.setProperty(str, LoadPropertie(file, str));
    				}
    			}
    		}
    		//Adding new properties line.
    		properties.setProperty(key, value);
    			try {
    				FileOutputStream out = new FileOutputStream("plugins/UnixRPG/" + file + ".properties");
    				properties.store(out, key + ": " + value);
    				out.close();
    				out.flush();
    		} catch (IOException e) {}
    		properties.clear();
    	}
     
    	public String LoadPropertie(String file, String key){
    		Properties properties = new Properties();
    		String value = null;
    		FileInputStream in = null;
    		try {
    			properties.load(in = new FileInputStream("plugins/UnixRPG/" + file + ".properties"));
    			value = properties.getProperty(key);
    			in.close();
    		} catch (IOException e) {}
    		properties.clear();
    		return value;
    	}

    When i load/save Strings from my .propertie file it seems it dose not close the file properly. When my program tryes to delete the .propertie file it fails every time.

    This is the part of the code where i need to delete the file:

    	public boolean onRemove(Player player, Location location){
    		World world = location.getWorld();
    		int x = (int) location.getX();
    		int y = (int) location.getY();
    		int z = (int) location.getZ();			
    		File file = new File("plugins/UnixRPG/signs/toggleblocks/" + world.getName() + "," + Integer.toString(x) + "," + Integer.toString(y) + "," + Integer.toString(z) + ".propertie");
    		boolean sucsess = file.delete();
    		if (sucsess == true){
    			Material1.remove(location);
    			Material2.remove(location);
    			FirstBlockLocation.remove(location);
    			LastBlockLocation.remove(location);
    			return true;
    		}
    		else {
    			player.sendRawMessage(ChatColor.RED + "ERROR: Failled deleting the toggleblocks file.");
    			return false;
    		}
    	}

    It says "ERROR: Failled deleting the toggleblocks file." every time.
    Last edited by ParkourGrip; January 15th, 2012 at 06:25 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: .propertie files...

    A comment on your programming:
    catch (IOException e) {}

    You should always call printStackTrace() in the catch blocks to show any errors. Your code ignores any errors.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: .propertie files...

    A comment on your programming:
    catch (IOException e) {}

    You should always call printStackTrace() in the catch blocks to show any errors. Your code ignores any errors.
    Ok i added it in SavePropertie method only becose i whant the LoadPropertie to return null if file has not be found or propertie line has not been found. That dose not solve the problem.
    Last edited by ParkourGrip; January 15th, 2012 at 08:13 AM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: .propertie files...

    Quote Originally Posted by ParkourGrip View Post
    Ok i added it in SavePropertie method only becose i whant the LoadPropertie to return null if file has not be found or propertie line has not been found. That dose not solve the problem.
    Might be the one of possibility that your resources are not getting free. Means, you are not closing your file and try to delete it while it is in use. Well, paste the error/exception messages here in order you have. Else look at your file writing code.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: .propertie files...

    Quote Originally Posted by Mr.777 View Post
    Might be the one of possibility that your resources are not getting free. Means, you are not closing your file and try to delete it while it is in use. Well, paste the error/exception messages here in order you have. Else look at your file writing code.
    The program shows no errors. It just dose not delete the file, puts in sucsess variable false, and as you see in my code prints to player "ERROR: Failled deleting the toggleblocks file.".

    And if i want to add printStackTrace() in LoadPropertie i need to test the reason for calling catch. If the file dose not exsist or if the propertie line dose not exist i need to return null. Not to print the error. If thats not the case i need to call printStackTrace().

    You can tell me how to test that if you know.
    Last edited by ParkourGrip; January 16th, 2012 at 05:14 AM.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: .propertie files...

    File file = new File("plugins/UnixRPG/signs/toggleblocks/" + world.getName() + "," + Integer.toString(x) + "," + Integer.toString(y) + "," + Integer.toString(z) + ".propertie");
    properties or propertie????
    Also, are you sure it's successfully making new file.

  7. The Following User Says Thank You to Mr.777 For This Useful Post:

    ParkourGrip (January 16th, 2012)

  8. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: .propertie files...

    Ok as others said about your exception handling..
    Always Catch Exceptions

    Also, the reason it wouldn't show any errors/exceptions is because you don't seem to ever use printStackTrace, which is how you find and diagnose your errors.

  9. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: .propertie files...

    Yeah it shood be ".properties". Thx that's such a stupid mistake. Ill see if it works. I cant even believe where i did that mistake. Ill see if it works then replay again.

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: .propertie files...

    Yeah it works now perfectly. Thx

Similar Threads

  1. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM
  2. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  3. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  4. need help on jar files
    By kaka09 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 13th, 2010, 09:30 AM
  5. hlp files
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 9th, 2010, 09:11 AM