Results 1 to 9 of 9
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default Opening any file of "specified extension" by a Java application

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    public class Main {
    
        private Scanner input;
    
        public void openFile() {
            try {
                [COLOR="Red"]input = new Scanner(new File("test.sk"));[/COLOR]
                javax.swing.JOptionPane.showMessageDialog(null, input.next());
            } catch (FileNotFoundException fe) {
                System.err.println("Error opening file");
                System.exit(1);
            }
        }
    
        public void closeFile() {
            if (input != null) {
                input.close();
            }
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            m.openFile();
            m.closeFile();
        }
    }

    Hey guys I created a file of extension ".sk" containing some text.... now i created an installer of the application i created above using "Advanced Installer" then i right clicked the same file from my drive and opened it with the application installed ... I got open:cool:... but now i want to open any file of the same extension ..just by right clicking or by associating the application to this file extension ... but i don't know how to specify any file of the same extension in the above code in red..

    plz suggest. thanx

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

    Default

    Unless I'm misinterpreting your goals, I think that you have to futz with operating system settings to achieve this.

  3. #3
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    I can't use
    input = new Scanner(new File("*.sk"));

    instead of
    input = new Scanner(new File("test.sk"))


    Isn't there any possible way to achieve this??

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

    Default

    If I understand you correctly, you want to double click or right click on an .sk file and have it run using your program. Is this correct?

    If so, then perhaps you can configure the OS to call your program whenever an .sk file is double clicked, and have the OS pass the file's name into your program's command line. Then the program has to be configured to accept the file from the command line and use it correctly, and also throw an error and exit if the file passed is not a valid .sk file. Would this work for you?

  5. #5
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    have the OS pass the file's name into your program's command line
    How would I do that .. plz.explain.

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

    Default

    Quote Originally Posted by Y. Progammer View Post
    How would I do that .. plz.explain.
    This is not a Java issue but an OS issue and is OS specific, meaning it varies from one OS to another. If you Google, you'll find out how.

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

    Default

    For instance, if it is a Windows environment, I think what you need to do is go into windows explorer, right click on a .sk file, any .sk file, click Open With, and change the default program that should open this file. I have no idea how to do this with other OS's.

  8. #8
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default [solved]

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    public class Main {
    
        private Scanner input;
        public static String str = "";
    
        public void openFile() {
            try {
                input = new Scanner(new File(str));
                javax.swing.JOptionPane.showMessageDialog(null, input.next());
            } catch (FileNotFoundException fe) {
                System.err.println("Error opening file");
                System.exit(1);
            }
        }
    
        public void closeFile() {
            if (input != null) {
                input.close();
            }
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            str = args[0];
            m.openFile();
            m.closeFile();
        }
    }

    YOu are absolutely right. It worked!! thnx buddy. I would for sure look for other OS's ..thnx.

    By the way .. can you help me solving my last post-
    Unable to render printing the complete JPanel
    gov.sns.tools.math.ElementaryFunction
    these are still unresolved!plz.

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

    Default

    Quote Originally Posted by Y. Progammer View Post
    YOu are absolutely right. It worked!! thnx buddy. I would for sure look for other OS's ..thnx.
    You're quite welcome.

    By the way .. can you help me solving my last post-

    these are still unresolved!plz.
    I wish I could, but you'll need more of a graphics wiz like Rob Camick or Darryl to help you solve that one. Did you post a decent SSCCE in that thread? If not, doing so will increase your chances of getting decent help fast. Best of luck!

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. Replies: 8
    Last Post: 12-30-2009, 12:40 PM
  3. Java "application infrastructure"
    By id_powell in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 01:46 PM
  4. Replies: 5
    Last Post: 01-27-2009, 04:49 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •