Open any file from within a java program
Asked Answered
C

2

6

Opening files in java seems a bit tricky -- for .txt files one must use a File object in conjunction with a Scanner or BufferedReader object -- for image IO, one must use an ImageIcon class -- and if one is to literally open a .txt document (akin to double-clicking the application) from java, this code seems to work:

import java.io.*;

public class LiterallyOpenFile {
    public static void main(String[] args) throws IOException {
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec("notepad Text.txt");
    }
}

I'm not positive, but I think other file-types / names can be substituted in the parenthesis after exec -- anyway, I plan on opening certain files in a JFileChooser when the user clicks on a file to open (when the user clicks on a file, the path to the file can be obtained with the getSelectedFile() method). Though I'm more specifically looking to be able to open an Arduino file in the Arduino IDE from a java program, like a simulated double-click.. perhaps something like this?

import java.io.*;

public class LiterallyOpenFile {
    public static void main(String[] args) throws IOException {
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec("Arduino C:\\Arduino\\fibonacci_light\\fibonacci_light.ino");
    }
}

A point in the right direction would be appreciated.

Culhert answered 15/12, 2014 at 3:13 Comment(1)
On Windows, you can always use start to open any file using its default file assocation. So instead of notepad or arduino or what not, replace all of them with start.Gabbi
S
7

Have you tried this? If there is a registered program for your file in windows, this should work. (i.e. the default application should open the file)

Desktop desktop = Desktop.getDesktop();
desktop.open(file);

The file parameter is a File object.

Link to API

Link to use cases and implementation example of the Desktop class

Skippy answered 15/12, 2014 at 3:58 Comment(3)
@Culhert I'm not sure if you're being sarcastic on this answer, but as far as I understand your question, this is the correct answer. read up the documentation of the Desktop class:docs.oracle.com/javase/7/docs/api/java/awt/Desktop.htmlUnsex
@Christian R. Wasn't sarcasm, just something odd involving arduino files: imgur.com/xQaYvy5 meaning the arduino file itself has to be located inside of a folder inside the desktop -- so I can use the Desktop class to open the folder that contains the file, but not the file directly. & thanks, I plan on reading up on the Desktop class.Culhert
you mix up the folder "Desktop" (what it is called in windows and other OS') with the class name, but for your challenge it fits perfectly - the file does not have to be inside the Desktop folder - the parameter could be any file object that is: not null, the file exists, is readable by security constraints and there is a associated application that could be launched with the file - if you take the file object from your JFileChooser.getSelected() as parameter, the file itself should be opened with the associated applicationUnsex
R
1

This is what I do in my projects using java.awt.Desktop

import java.awt.Desktop;
import java.io.IOException;
import java.io.File;    
public class Main
{
     public static void main(String[] args) {
        try {
              Desktop.getDesktop().open(new File("C:\\Users\\Hamza\\Desktop\\image.png"));
            } catch (IOException e) {
              e.printStackTrace();
            }
    }
}
Redeem answered 22/2, 2021 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.