If you want to open a file using the default registered or associated application for those files you can use the Desktop.open(File file)
method call. In the example below we’ll ask the Desktop
class to open a text file.
package org.kodejava.awt;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class RunningDefaultAppOpen {
public static void main(String[] args) {
// A reference to a text file
File file = new File("data.txt");
try {
Desktop desktop = Desktop.getDesktop();
// Open a file using the default program for the file type.
// In the example we will launch a default registered
// program to open a text file. For example on Windows
// operating system this call might launch a notepad.exe
// to open the file.
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
How to do with webapplication? I want to download file from server and open in native default registered application.