Results 1 to 8 of 8
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default saving a string data.

    I wanted to add a save option to my program. I made a "save()" methode and in that methode I rewrote all the data I need to save into one (big) String. Now I want to find if there is a text file in the same map as the program with the name I give my text files (for example "saved data" or something like that). If there is, I want to rewrite it's content to be the String I made. If there isn't such a file, I want to make it and then fill it with the String.

    My problem thus basicly boils down to: how do you find,rewrite and create text files with java?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for FileWriter.

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    That helped, but how do I get the pathname of the place where the program resides? I need that to tell the file object where to point to.

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    So most of want I want to be done would be achieved by this code:
    Java Code:
    		File F=new File();//<--gives error
    		F.delete();
    		F.createNewFile();
    		FileWriter FW=new FileWriter(F);
    		FW.write(s);
    Where my String is called "s". Only I don't have a path to give to the file, because I don't know where the program "lives".

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by imorio View Post
    Only I don't have a path to give to the file, because I don't know where the program "lives".
    Maybe the System Properties object can be of help; read which properties are defined (they're listed in the API documentation for the System.getProperties() method). Hint: look at the "user.dir" property.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by imorio View Post
    Only I don't have a path to give to the file, because I don't know where the program "lives".

    use this example to get the path of the class. please note to replace the class name before the method call class.getProtectionDomain().getCodeSource().getLoc ation() if you use it in an other class. btw: if you don't give any path your file will be stored in the 'current' path.

    Java Code:
    import java.io.File;
    import java.net.URL;
    
    
    public class getClassPath {
    
    	public static void main(String[] args) {
    		URL mySource = getClassPath.class.getProtectionDomain().getCodeSource().getLocation();
            File sourceFile = new File(getClassPath.getPath());
            System.out.println(sourceFile);
    	}
    }

  7. #7
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    My code currently looks like this for saving String s and returning a boolean regarding it's succes:

    Java Code:
    try
    {
    	Properties P=System.getProperties();
    	File F=new File(P.getProperty("user.dir"));
    	F.delete();
    	F.createNewFile();
    	FileWriter FW= new FileWriter(F);
    	FW.write(s);
    }
    catch(IOException e)
    {
    	return false;
    }
    catch(SecurityException e)
    {
    	return false;
    }
    return true;
    Anything I missed?
    Last edited by imorio; 11-18-2010 at 10:44 AM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by imorio View Post
    My code currently looks like this for saving String s and returning a boolean regarding it's succes:

    Java Code:
    try
    {
    	Properties P=System.getProperties();
    	File F=new File(P.getProperty("user.dir"));
    	F.delete();
    	F.createNewFile();
    	FileWriter FW= new FileWriter(F);
    	FW.write(s);
    }
    catch(IOException e)
    {
    	return false;
    }
    catch(SecurityException e)
    {
    	return false;
    }
    return true;
    Anything I missed?
    You're trying to delete the user's entire current working directory; that'll probably fail because most likely that directory won't be empty. Is that really what you had in mind?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Hi! Saving data??
    By Gahstanz in forum New To Java
    Replies: 1
    Last Post: 06-29-2010, 06:29 AM
  2. What do you use for saving data from table?
    By ocean in forum New To Java
    Replies: 1
    Last Post: 11-21-2009, 07:08 PM
  3. Problem with hobernate Saving Data
    By Prashant.surwade in forum Java Servlet
    Replies: 2
    Last Post: 10-28-2009, 06:34 AM
  4. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 03:14 AM
  5. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •