S
S
Sergei Sanchez-Perez2021-07-10 23:01:38
Java
Sergei Sanchez-Perez, 2021-07-10 23:01:38

How to use java code to open a text file for example?

When you run the code, a text file should open, or, in general, any file that is specified in the path. Or open a browser. Is this possible to do? If yes, then give an example for Windows, I will be very grateful!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-07-11
@SANCHEXAS

To the question of running Windows programs through Java, you can look here:
How to run programs through cli on Windows - https://superuser.com/questions/494538/how-do-i-op...
To the question of how to execute commands in cmd - https://stackoverflow.com/questions/15464111/run-c... Have a
look at the Process & ProcessBuilder classes. The link above shows an example of their use.

Or open a browser.

Depends on what purpose you want to open the browser. If further work with the browser from the code is meant (for example, testing, parsing or something else ...), then take a look at Selenium.
https://www.browserstack.com/guide/run-selenium-te...
Google -
java selenium {your_browser}

K
kiryatpechin, 2021-07-13
@kiryatpechin

Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
    desktop = Desktop.getDesktop();
}
desktop.open(new File(filename));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question