Results 1 to 13 of 13
  1. #1
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Question [SOLVED] File chooser selecting file from directory...?

    Hi All,

    While selected a file from FileChooser, file name along with its extension is retrieved, but for me i need only the filename;
    For example:
    if my file name is 'name.log' and if i select the file name from the directory, i should get only the file name 'name' alone instead, im getting the file name with its extension as 'name.log' and if i save it, it consider it as a new file name and saves it as 'name.log.log' as a new file and file already exist error is not thrown.

    Looking forward for reply.

    Regards,
    Prabhu.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Eranga,

    Im java.io.File to get the file name from JfileChooser;
    File fileName = chooser.getSelectedFile();

    and how to do substring with this, if it is a string type then we can do substring.

    Looking forward for your reply.

    Regards,
    Prabhu.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Yes you are right eranga, but this gives the file path with the extension of the file also; if i select the file name that is already exist; example 'test' is my file name and its extension is '.txt' and if i select the file 'test', from the save dialog file directory, it retrieves the file name with its extension, 'test.txt' and treats it as a new file and if i click save it is not throwing any error, it saves as 'test.txt.txt'.

    Eranga i hope you could get me, of what im trying to explain you.

    Looking forward for your reply.

    Regards,
    Prabhu.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ya, I get you. fileName is an object reference of File right? So what you can do?

    Java Code:
                    File fileName = chooser.getSelectedFile();
                    String temp = fileName.toString();
                    String name = temp.substring(temp.lastIndexOf("\\") + 1, temp.lastIndexOf("."));
                    System.out.println(name);

  7. #7
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Thank you Eranga; this is what i need, but can you please go through my last post, again im facing that issue.


    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    int actionDialog = chooser.showSaveDialog(this);
    if ( actionDialog == JFileChooser.APPROVE_OPTION )
    {
    //File fileName = new File( chooser.getSelectedFile( ) + ".log" );
    File fileName = chooser.getSelectedFile();
    String temp = fileName.toString();
    String name = temp.substring(temp.lastIndexOf("\\") + 1, temp.lastIndexOf("."));
    //File file_name = new File(name);
    //System.out.println(name);
    if(fileName.exists())
    {
    int confirmDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
    if (confirmDialog == JOptionPane.NO_OPTION)
    {
    chooser.showSaveDialog(this);

    }
    if(actionDialog == JOptionPane.YES_OPTION)
    {
    BufferedWriter outFile = new BufferedWriter( new FileWriter( name ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }

    //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists"));
    }
    How to use this modified file name in our dialog, when a file name is selected.

    Regards,
    Prabhu.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Yes Eranga,
    I need my save as dialog to perform, as it is in a wordpad document. I think im saying again and again the same grinded flour. Can you please check a word document and try to save it with existing file name. The same logic i need it in java swing, while saving a file.

    Regards,
    Prabhu.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Check the simple logic, just written this and you have to change according to your application.

    Java Code:
        private void workignWithChooser() {
            try {
                JFileChooser chooser = new JFileChooser(new File("./"));
                boolean isValid = true;
                
                do{
                    int chooserStat = chooser.showSaveDialog(this);
    
                    if(chooserStat == JFileChooser.APPROVE_OPTION) {
                        // Do the process
                        File userFile = chooser.getSelectedFile();
                        if(userFile.exists()) {
                            System.out.println("File exist");
                            int result = JOptionPane.showConfirmDialog(this, 
                                    "Replace existing file?");
                            if(result == JOptionPane.YES_OPTION) {
                                // Save and exit
                                isValid = false;
                            }
                            else if(result == JOptionPane.NO_OPTION) {
                                //any logic if you have
                            }
                        }
                        else {
                            System.out.println("file is not exsit");
                        }
                    }
                    else if(chooserStat == JFileChooser.CANCEL_OPTION) {
                        System.exit(0);
                    }
                }while(isValid);
                System.exit(0);
            }
            catch(Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }

  11. #11
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Thank you very much Eranga, I think you have given me the exact solution, let me check it and let you know.

    Regards,
    Prabhu.

  12. #12
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Thank you very much Eranga. I got my solution, after making some modifications to your code. I will get back to you on further post.

    Bye take care.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Is there a way to read a file directory
    By willemjav in forum Java Applets
    Replies: 3
    Last Post: 09-05-2011, 01:01 AM
  2. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:14 AM
  3. Using File Chooser
    By shaungoater in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 12:30 PM
  4. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:02 PM
  5. How to Move a File to Another Directory
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:17 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
  •