Results 1 to 8 of 8
Thread: saving a string data.
- 11-17-2010, 10:07 AM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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?
- 11-17-2010, 10:27 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
See the API docs for FileWriter.
- 11-17-2010, 11:58 AM #3
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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.
- 11-17-2010, 12:53 PM #4
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
So most of want I want to be done would be achieved by this code:
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".Java Code:File F=new File();//<--gives error F.delete(); F.createNewFile(); FileWriter FW=new FileWriter(F); FW.write(s);
- 11-17-2010, 12:59 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-17-2010, 01:20 PM #6
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); } }
- 11-18-2010, 10:41 AM #7
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
My code currently looks like this for saving String s and returning a boolean regarding it's succes:
Anything I missed?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;Last edited by imorio; 11-18-2010 at 10:44 AM.
- 11-18-2010, 11:33 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Hi! Saving data??
By Gahstanz in forum New To JavaReplies: 1Last Post: 06-29-2010, 06:29 AM -
What do you use for saving data from table?
By ocean in forum New To JavaReplies: 1Last Post: 11-21-2009, 07:08 PM -
Problem with hobernate Saving Data
By Prashant.surwade in forum Java ServletReplies: 2Last Post: 10-28-2009, 06:34 AM -
Saving data...?
By easyRyder in forum New To JavaReplies: 8Last Post: 07-15-2008, 03:14 AM -
Saving data in an XML file
By Thez in forum New To JavaReplies: 1Last Post: 12-08-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks