Save a file with the option to open it - java

I am looking for a solution to save a file in a Java (Eclipse RCP) application with the option to open the file right after saving it. Similar is when a browser saves a dowloaded file it allowes to open it. A simple FileDialog has no such option.

I think, you need to open the file yourself after you save it.
Do the following (in windows):
String cmd = "cmd.exe /c start ";
String file = "path\version.txt"; //TODO: your file path here
Runtime.getRuntime().exec(cmd + file);
Similar approach can be applied for linux environment. Search for it yourself.
If you need the option (may be a checkbox) in JFileChooser, you can extend JFileChooser and add your features.

Related

How to open .txt using windows explorer context menu in java swing?

I am making a text editor for .html, .CSS, .js, .txt etc.
First I made my software in java swing and then I changed it to jar file then I changed it to exe file using launch4j and then I made it setup using innosetup but if I install this software and I right click on a file in windows explorer and choose open with and then I select my software and open it, it just open my software not the selected file so how to do it?
Windows will supply the path to the file as args[0] in your main method.
if(args.length > 0) {
Path txt = Path.of(args[0]);
//code to handle
}

How To Open Files With .Jar (Java) Application?

Tough question here. How do you open a file in java, in the way that when you double click on the file it automatically opens in a java application.
I'm making a musicplayer (first real big Java project for me) and I have no clue how to acheive this. When you open, lets say, a .mp3 file it will open in whatever default program you have selected for it (such as VLC mediaplayer or Windows Media Player). What I want is to be able to set the .jar file of my application as the default program for .mp3 files, and then to be able to actually launch the files in the application.
When I currently try to open a file with the application I get a windows error saying "This app cannot be executed on your pc". But when I launch the .jar itself without doing it by trying to open a .mp3 file it runs just fine.
Does anyone know how to acheive what I want? Many thanks in advance!
---edit---
I do not mean that you can select a default program for the mp3 file. The problem is that windows throws the error shown above, and that I dont know how to handle the application being launched by opening a file (which does not ope due to the error).
I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)
BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:
if (args.length > 0) {
File f = new File(args[0]);
start_the_application_with_a_file(f);
} else {
start_the_application_without_a_file();
}
C:\ProgramData\Oracle\Java\javapath\java.exe -jar "C:\Program Files\YourApp.jar" %* within a batch-file (.cmd) might do it.

Java file names that can work for both unix and windows

My need is to open a file (temp file) and write something to it
How can I do it so that it would work for all the OS types (at least for unix and windows)
Below is my current code
Every time, I want to test something on windows, I will be to toggle between these lines (comment/uncomment)
//File file = new File("C:\\PM_DELETE_CARRIER_TEST_FOLDER\\"+carrier.getCarrierId()+"Carrier_BackUp_Restore.sql");
File file = new File("/app/jakarta-tomcat/logs/Carrier_BackUps/"+carrier.getCarrierId()+"Carrier_BackUp_Restore.sql");
You can make use of the System properties in Java. Say suppose Use "user.home" in your file path so that the file will be placed in the user home directory. You don't have to switch in between
Ex:
File file = new File(System.getProperty("user.home")+File.separator+
"Carrier_BackUp_Restore.sql");
Adding another way by #EJP comment to avoid file separator
File file = new File(System.getProperty("user.home"), "Carrier_BackUp_Restore.sql");
For a temporary file, you can let the JVM choose a suitable path for you: see File.createTempFile.
"My need is to open a file (temp file) and write something to it" .
Why not use Files.createTempFile() but remember the file is not automatically deleted , You can use deleteOnExit() or open it via DELETE_ON_CLOSE options. If you want to create the file manually then use Paths
Paths.get("foo", "somelocation").toFile();

Eclipse plugin and editor

How to open a file outside of the project programically in a new editor window ? I've been searching all over the internet and nothing seems to work.
I have eclipse 4.4.
I thought something so simple as opening a file will be easy but somehow it keeps me awake at night.
Please help.
I cannot comment as yet, so adding my question/suggestion as an answer.
What do you mean by your question? You mean something like a command to open a target file in some editor that is installed on your computer.
You could try using
Process process = Runtime.getRuntime ().exec ("<path to editor>/some_editor.exe");
If the program allows taking file to open as parameter, you could place that too in commmand above towards the end.
I hope this is what you are looking for.
To open an editor on a file which is not in the current workspace use:
String path = ... path to file
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IDE.openInternalEditorOnFileStore(page, fileStore);
Note: some editors may not support files that are not in the workspace.

Dropdown Menu Opening Bat File

I need some help.I have a Java program with a drop-down menu,there is an option in that menu to launch a test.bat file, what code would I use to specify the system path for it to open test.bat? Test.bat is an external file for testing purposes.
EDIT
This problem has been resolved.
File f = new File("test.bat");
Desktop.getDesktop().open(f);

Categories