Results 1 to 6 of 6
  1. #1
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Preferred method for saving/accessing text files

    is there a preferred location to store files generated by the application?
    It's up to the application. Putting the file in the same folder as the jar file is simpler.

    save a text file in my package
    What do you mean by "my package"? Is that a folder in your IDE?

    way to access text files for editing/saving
    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.

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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:
    Java Code:
    			File file = new File("/my/local/directory/SQL.txt");
    			FileReader fr = new FileReader(file); 
    			BufferedReader br = new BufferedReader(fr);
    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:
    InputStream is = DatabaseActions.class.getResourceAsStream("SQL.txt");
    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.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Preferred method for saving/accessing text files


  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Preferred method for saving/accessing text files

    I am not sure where my txt file needs to be in order for this to work
    Use the getResource() method to return a URL and print it out to see the location.
    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.

  6. #6
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Preferred method for saving/accessing text files

    Quote Originally Posted by Norm View Post
    System.out.println("loc=" + TestCode8.class.getResource("TestCode8.java"));
    Worked..thanks!

Similar Threads

  1. Handling multiple WAV files and saving them into ONE
    By phil128 in forum Advanced Java
    Replies: 2
    Last Post: 12-11-2010, 02:31 PM
  2. Replies: 7
    Last Post: 12-03-2010, 04:47 AM
  3. Need help with accessing files in a method
    By t0nydanzuh in forum New To Java
    Replies: 6
    Last Post: 10-03-2010, 06:05 PM
  4. regarding saving files or objects in database
    By sandeepsai39 in forum New To Java
    Replies: 10
    Last Post: 08-28-2010, 06:09 AM
  5. Saving to a Text File
    By jadaleus in forum Advanced Java
    Replies: 2
    Last Post: 10-17-2008, 06:50 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
  •