4

I am trying to figure out if there is any platform independent way to open a text file with the default text editor even if the file doesn't end in .txt. I tried following code but it throws exception for files not having .txt extension. Works perfectly fine for .txt files.

       Desktop dt = Desktop.getDesktop();
       try
       {
            dt.open(fileName);      
       }
       catch(Exception e){
          // Catch exception here 
       }
dimo414
  • 47,227
  • 18
  • 148
  • 244
ATHER
  • 3,254
  • 5
  • 40
  • 63
  • If there is no extension then OS can't be sure what kind of file it handles, so it can't chose appropriate application for it. Can't you just rename this file and add `.txt` suffix to it? Your other choice is to manually execute command invoking some build in text editor (vi, notepad, or others) depending on OS you are in. – Pshemo Aug 28 '14 at 22:03
  • There is an extension but it is not .txt. Yea the other choice is to write custom logic to specify editor for text file based onto the platform the program is running on. – ATHER Aug 28 '14 at 22:17
  • Have you tried using `Desktop.edit()`? Not sure if that would really work better, but it might work for a larger set of extensions. Part of the issue is there are clearly extensions (`.doc`, `.png`, `.psd`, etc.) that *should* not open in a text editor, but it's not clear how the distinction should be defined. – dimo414 Aug 28 '14 at 22:31
  • What about, read the file's name, if it ends with .txt, just launch the text editor, if not append .txt to its name, and try to open it with the text editor. Finally, regardless of whether it was successfuly opened by the text editor or not, rename the file to its original name. – Evdzhan Mustafa Aug 28 '14 at 22:32
  • @EvcanMustafa that would disconnect the file in the editor from the file on disk - `Desktop.open()` doesn't block until the user closes the editor, so there's no (easy) way to know when to rename the file back. – dimo414 Aug 28 '14 at 22:37

2 Answers2

2

Untested, never the less:

  • create a symbolic link via nio
    • with the right extension
    • in a temp dir
  • Call Desktop.open() on the symbolic link

This works for me locally:

public static void main(String... args) throws IOException {
    Path source = FileSystems.getDefault().getPath(args[0]);
    Path symLink = Files.createTempFile(source.getFileName().toString(), ".txt");
    Files.delete(symLink);
    Files.createSymbolicLink(symLink, source);
    Desktop.getDesktop().open(symLink.toFile());
}

Disclaimer: I have not done any research beyond the above smoke test.

Andreas
  • 4,937
  • 2
  • 25
  • 35
  • An interesting idea, can you expand on this? Do you have reason to believe `Desktop.open()` will open the editor associated with the symlink / Windows shortcut name? – dimo414 Aug 28 '14 at 22:54
  • `s/nio/noi/` This is a clever approach, and definitely worth a try. – David Conrad Aug 28 '14 at 23:29
  • I tried this, but for me the line Files.createSymbolicLink(symLink, source); throws exception with error message " required privilege is not held by the client.". I am using Windows 7 Professional. – ATHER Aug 29 '14 at 20:48
  • Works for me in Win7. See this question: http://stackoverflow.com/questions/8228030/getting-filesystemexception-a-required-privilege-is-not-held-by-the-client-usi – Andreas Aug 29 '14 at 20:59
-2

I am trying to figure out if there is any platform independent way to open a text file with the default text editor whatever it is.

Do you see the conflict above? All the platforms have their own ways to manage default text editors and there's no way for Java to find the default editor. Since your files are not .txt, you cannot use the nice methods from AWT.

However, you could

  • use File.renameTo() and add the .txt extension
  • open the directory containing the file

I'm aware that both methods (especially the first one) have their drawbacks. The first method actually moves the file and even a copy doesn't solve the problem effectively. However, the second approach might work.

I'm afraid you can't do more if you want to stay platform independent.

J4v4
  • 780
  • 4
  • 9
  • 3
    Calling it a conflict (or contradiction, more accurately) is a bit heavy-handed. It is clearly possible to launch the default `.txt` editor, it's not *unreasonable* to imagine that it'd be possible to specify opening a file as if it were a specific type, e.g. a fictional `Desktop.open(File, MimeType)` method. – dimo414 Aug 28 '14 at 22:53