Results 1 to 6 of 6
- 12-05-2011, 06:11 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Preferred method for saving/accessing text files
Hello, I have a question regarding the most correct way to access text files for editing/saving. My application currently just uses a local directory on my machine, but I need it to work on any machine once deployed. Is there a way to save a text file in my package and edit/save it later? I have looked at the getClass().getResource("fileName.ext") method, but this will not work with my static save method. If not, is there a preferred location to store files generated by the application? Thanks!
- 12-05-2011, 06:49 PM #2
Re: Preferred method for saving/accessing text files
It's up to the application. Putting the file in the same folder as the jar file is simpler.is there a preferred location to store files generated by the application?
What do you mean by "my package"? Is that a folder in your IDE?save a text file in my package
You can use the FileReader wrapped in a BufferedReader to read the file and use the corresponding set of classes named FileWriter and BufferedWriter to write them.way to access text files for editing/saving
- 12-05-2011, 07:02 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Preferred method for saving/accessing text files
Norm,
Thanks for the response. I think my question wasn't clear enough. I am currently using BufferedReader and BufferedWriter to access and edit my file, but I am referencing locally. Example:
I need to change "/my/local/directory/SQL.txt" to something meaningful for app deployment. From my googling I have found the following:Java Code:File file = new File("/my/local/directory/SQL.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr);
However I am not sure where my txt file needs to be in order for this to work. I have tried the application root and the folder that contains the .class files.Java Code:InputStream is = DatabaseActions.class.getResourceAsStream("SQL.txt");
- 12-05-2011, 07:08 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Preferred method for saving/accessing text files
- 12-05-2011, 07:15 PM #5
Re: Preferred method for saving/accessing text files
Use the getResource() method to return a URL and print it out to see the location.I am not sure where my txt file needs to be in order for this to work
For example This code:
System.out.println("loc=" + TestCode8.class.getResource("TestCode8.java"));
printed this:
loc=file:/D:/JavaDevelopment/Testing/ForumQuestions7/TestCode8.java
Make many copies of the SQL.txt file and put them in all the folders that are on the classpath when you execute the program. When the above code finds the file it will print out the URL showing the files location.
- 12-05-2011, 07:32 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Handling multiple WAV files and saving them into ONE
By phil128 in forum Advanced JavaReplies: 2Last Post: 12-11-2010, 02:31 PM -
Preferred method for passing toolbar events to JFrame
By jcleland in forum AWT / SwingReplies: 7Last Post: 12-03-2010, 04:47 AM -
Need help with accessing files in a method
By t0nydanzuh in forum New To JavaReplies: 6Last Post: 10-03-2010, 06:05 PM -
regarding saving files or objects in database
By sandeepsai39 in forum New To JavaReplies: 10Last Post: 08-28-2010, 06:09 AM -
Saving to a Text File
By jadaleus in forum Advanced JavaReplies: 2Last Post: 10-17-2008, 06:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks