How close a file opened by java.awt.Desktop - java

How to close an opened excel file?
Open excel code is:
File file = new File("e:\\aaa.xlsx");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
What would be the close code?
just using java open and close Windows application like use mouse.

Forget it. Open here means that the registered application, Excel, takes over and opens a Window. After that you have no control, but watching. Excel will close.
File is a class that represents a file system path. Itself it has no state of associated reader/writer. Java 7 now parallel introduces a more evolved class Path. Besides (obviously) the path on the file system, it also stores what file system. With Path one can have more than one FileSystem, like a ZipFileSystem. That allows you to copy and rename files in a zip.
That was just an elaboration.

Related

How can pass my java application (as .exe) the path of a document on startup?

I use JSmooth to turn my java application (jar) into an executable. It´s a simple text editor, like notepad. I want to use the "Open With" function of windows to open certain files with the exe. For this, I only need the path of each file. How do I do this?
I thought about using a java-property and calling it with System.getProperty("VariableName"), though I don´t know if this is possible. ${EXECUTABLEPATH} just gets me the location of the MyApp.exe.
For short properties may use Preferences.userNodeForPackage(MyExample.class);
and methods: get, put, flush.
https://docs.oracle.com/javase/8/docs/technotes/guides/preferences/index.html
and/or to use properties file near your exe file for save paths
https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
For open with Windows may use this schema for example:
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN)) {
desktop.open(file);
}
}
https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html
For set associations see:
https://www.thewindowsclub.com/change-file-associations-windows
may be usefull
Have the ability to set Java application as default file opener?

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.

Open a file using Desktop(java.awt)

Am using the following code snippet to open a file,
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
File myFile = new File(fileName);
desktop.open(myFile);
} catch (IOException ex) {}
}
if the file i try to open is of normal .txt or .pdf any such file types i can able to open the file.
But in some cases,
As shown above the Type of file is File , in this scenario the file is not opened.
if i manually open this File type file i get open with window and using a text editor i am able to open it.
How should i handle this in java? Please help..
Windows says the type of the file is "file" when there is no extension. This is why there is no default opener, and that is why Java doesn't open the file properly.
You can either rename the file and give it an extension, or use Java to directly execute the program you want to open the file with.
#open method finds the specified default app to open the specified file but in .file type there is no default app to open the file.You can only specify manually to open it in specific app.So if you set the default application that should open the .file and you have to set the default application and choose default program to open the file and than run the program.

location for notepad in android

I had created a notepad file automatically using OutputStreamwriter.
I just want to ask where is the location of the notepad file in my android project?
If ever there is a way to customize the location of that notepad file, may I know how?
OutputStreamWriter osw = new OutputStreamWriter(openFileOutput("playersdata.txt",MODE_APPEND));
openFileOutput("playersdata.txt",MODE_APPEND)
Creates a FileOutputStream based on a File in the application's private folder of the internal storage.
To determine the actual path of that file, you can use the method to get a corresponding File object, and then get it's absolute path:
getFileStreamPath("playersdata.txt").getAbsolutePath()
Note that unless you alter the mode settings, you will not be able to interact with this file except from code that runs either as the application's package (by being part of it, or using the run-as shim on a debug APK, on a device where that isn't broken), or as root on an emulator/rooted device. Another approach is to build functionality into your program to copy private files out to external storage so that you can examine them during the development stage.

Can't find folder or file created with Android App with windows file exlorer

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.

Categories