Results 1 to 5 of 5
  1. #1
    Raidenji is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default How to save path of a file

    Hello,

    As i am very new to programing i wasn't sure where to post this question.

    I have a program to open a file. Doesnt do anything at the moment, What i need to know is how to save the "PATH" of the file to a variable to use later. THis is what i have right now, not sure how to go about this.

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		JFileChooser chooser = new JFileChooser();
    		chooser.setCurrentDirectory(new File("dir"));
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    		chooser.showOpenDialog(frame);
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: How to save path of a file

    If you have a File object you will always be able to access its Path.

  3. #3
    Raidenji is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to save path of a file

    I have figured it out somewhat. Still need help though. This is only returning the Current Directory. what i need is the full PATH. As in c:\folder\folder\file.txt

    Java Code:
    public void actionPerformed(ActionEvent e) {
    				JFileChooser chooser = new JFileChooser();
    				chooser.setCurrentDirectory(new File("dir"));
    			    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    				chooser.showOpenDialog(frmndVirtualFighter);
    				//File selFile = chooser.getSelectedFile();
    				File selFile = chooser.getCurrentDirectory();
    				String pathName;
    				pathName = chooser.getName(selFile);
    				
    				JOptionPane.showMessageDialog(frame, pathName, "About", JOptionPane.PLAIN_MESSAGE);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to save path of a file

    As Junky suggests, use the JFileChooser to select the File, but not to get the path String. Instead call one of the several available File methods on the chosen file, here selFile, to get the path String, methods such as getCanonicalPath() or one of the several others that you can find on the File API.
    Last edited by Fubarable; 11-22-2012 at 02:23 AM.

  5. #5
    Raidenji is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to save path of a file

    Thank you guys for your quick response. getCanoniclePath() worked perfectly

Similar Threads

  1. (in Java mysql to excel) where i have specify the path to save the file
    By raj.mscking@gmail.com in forum New To Java
    Replies: 3
    Last Post: 06-04-2012, 10:23 AM
  2. Java save to file / open from file - trouble
    By joelBeaton in forum Advanced Java
    Replies: 6
    Last Post: 03-23-2012, 12:47 PM
  3. Replies: 3
    Last Post: 06-21-2011, 04:51 PM
  4. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 11:17 AM
  5. Replies: 0
    Last Post: 03-05-2010, 04:21 AM

Posting Permissions

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