2

I have a files list. Lets say it looks:

String[] lst = new String[] {
    "C:\\Folder\\file.txt", 
    "C:\\Another folder\\another file.pdf"
};

I need some method to open these files with default program for them, lets say "file.txt" with Notepad, "another file.pdf" with AdobeReader and so on.

Does anyone knows how?

tshepang
  • 12,111
  • 21
  • 91
  • 136
ST3
  • 8,826
  • 3
  • 68
  • 92

5 Answers5

4

There is a method to do this:

java.awt.Desktop.getDesktop().open(file);

JavaDoc:

Launches the associated application to open the file.

If the specified file is a directory, the file manager of the current platform is launched to open it.

Xeon
  • 5,949
  • 5
  • 31
  • 52
1

The Desktop class allows a Java application to launch associated applications registered on the native desktop to handle a URI or a file.

Miguel Prz
  • 13,718
  • 29
  • 42
1

If you are using J2SE 1.4 o Java SE 5, the best option is:

for(int i = 0; i < lst.length; i++) {
    String path = lst[i];
    if (path.indexOf(' ') > 0) { 
        // Path with spaces
        Runtime.getRuntime().exec("explorer \"" + lst[i] + "\"");
    } else { 
        // Path without spaces
        Runtime.getRuntime().exec("explorer " + lst[i]);
    }
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • This assumes windows is the O/S. Other answers have shown how to do this in a cross-platform way – tddmonkey Apr 14 '13 at 19:13
  • @MrWiggles I think it is a fair assumption (based on the question's file paths), and this answer does have its value as it points an alternative to `java.awt.Desktop`, although it does need a tweek to work on other (non-windows) systems. – acdcjunior Apr 14 '13 at 19:17
  • @MrWiggles `java.awt.Desktop` was introduced in Java SE 6. But wait, you could edit the answer for other operating systems. – Paul Vargas Apr 14 '13 at 19:40
0

Just make sure the file is in the right location, and this should work fine.

try
{
    File dir = new File(System.getenv("APPDATA"), "data");
    if (!dir.exists()) dir.mkdirs();
    File file = new File(dir"file.txt");
    if (!file.exists()) System.out.println("File doesn't exist");
    else Desktop.getDesktop().open(file);
} catch (Exception e)
{
    e.printStackTrace();
}
syb0rg
  • 8,057
  • 9
  • 41
  • 81
0

I didn't know you have a String array now. So, this one uses regex to process the file list in the format you specified before. Ignore if not required.

If the file list is huge and you would prefer that the files open one by one cmd works great. If you want them to open all at once use explorer. Works only on Windows but then on almost all JVM versions. So, there's a trade-off to consider here.

public class FilesOpenWith {

    static String listOfFiles = "{\"C:\\Setup.log\", \"C:\\Users\\XYZ\\Documents\\Downloads\\A B C.pdf\"}";

    public static void main(String[] args) {
        if (args != null && args.length == 1) {
            if (args[0].matches("{\"[^\"]+\"(,\\s?\"[^\"]+\")*}")) {
                listOfFiles = args[0];
            } else {
                usage();
                return;
            }
        }
        openFiles();
    }

    private static void openFiles() {
        Matcher m = Pattern.compile("\"([^\"]+)\"").matcher(listOfFiles);
        while (m.find()) {
            try {
                Runtime.getRuntime().exec("cmd /c \"" + m.group(1) + "\"");
                // Runtime.getRuntime().exec("explorer \"" + m.group(1) + "\"");
            } catch (IOException e) {
                System.out.println("Bad Input: " + e.getMessage());
                e.printStackTrace(System.err);
            }
        }
    }

    private static void usage() {
        System.out.println("Input filelist format = {\"file1\", \"file2\", ...}");
    }

}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89