[Java] Opening a file that does not have an extension

Asked by Boipelo Mawasha

Is it possible to open a file that has no extension by specifying the program that you want to use to open the file?

E.g.

open \\sever\\folder\\filename with notepad++

The file that I create is not saved with an extension. When I open it manually, I have to choose a program to open it with

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

This should do what you want:

import os
dir = r"C:\Users\Raimund Hocke" # specify your own folder
path = os.path.join(dir, "some-file")
os.popen('start notepad.exe "%s"'%(path))

Revision history for this message
RaiMan (raimund-hocke) said :
#2

Uuups, it is Java that you requested, sorry ;-)

Use the according features, to run the start command from within Java.

Revision history for this message
Boipelo Mawasha (bmawasha) said :
#3

I keep getting this error when I try the code out in Sikuli:

[error] An error occurs at line 3
[error] Error message: SyntaxError: ("mismatched character '\\n' expecting '''",

Line 3 is: dir = r"C:\test\"

Revision history for this message
Boipelo Mawasha (bmawasha) said :
#4

My bad. My typing is horrible. I'm sorted, thanks

Revision history for this message
Boipelo Mawasha (bmawasha) said :
#5

Thanks RaiMan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

a raw string r"" cannot have a backslash as last character.

That's why I am using os.path.join(), which builds a correct path without the need for the additional \

so use:
dir = r"C:\test"

Revision history for this message
RaiMan (raimund-hocke) said :
#7

*** from other question: /// pls. stay here

I need to get access to methods from org.sikuli.script.OS; in Java.

import org.sikuli.script.OS;

//Field
private OS myOS;.

public void Test(){

tempString = "filename";
  dir = "\\\\server\\folder\\folder\\";
  path = os.path.join(dir, tempString);
}

I get an error when I try to use os.path.join

Shouldn't I get access to all the methods in the class after I use the import statement?

There seems to be no other way. I tried a work around, but it failed. It's either I succeed with opening the file or opening the application. Never both.

Revision history for this message
RaiMan (raimund-hocke) said :
#8

if you want to do this in Java, you have to use the Java methods to construct the correct path string.

The final string for the start command in Java should print out like this:

start notepad.exe "\\sever\folder\filename"

It would be wise, to first check from a command line wether it does what you want.

Then go back to Java, construct the command correctly and submit it from within Java.

Revision history for this message
Boipelo Mawasha (bmawasha) said :
#9

Thanks RaiMan. This syntax switching gets me at times. It's all coming together now