Play a file after calling getPath in Java - java

The return value of a value from getPath() from the File Class is something like this
"C:\Users\Daniel\Desktop\ASDF.mp3".
To use the Desktop class from java to play a file, the path would have to be fed into a file, with a path similar to
"C:\\Users\\Daniel\\Desktop\\ASDF.mp3"
Since the \ is a reserved character(From my understanding) to make a new file you must use a double backslash to dictate that it is a file. My problem is that when I try to get the path I need to transform it into a double slash version. The .replaceAll() method doesn't allow for '\' since it's a reserved character but the .replace() method does.
To work around this would I just have to loop through to find all instances and replace them one at a time? Or is there a simpler work around? Also I would like to know if I am receiving this error due to it being a reserved character, or if I am completely wrong.

The two strings above are actually exactly the same.
When Java or other language outputs a string it only displays one slash '\', but when you are typing the string into double quotes you need double backslash '\\' so the Java parser knows it's one slash. Backslash is used for many other escape characters, so this is only way parser will know.
(Even when typing this answer, I needed 4 backslashes to make only 2!

Related

Java Properties File to Use / Forward Slash in Key

I have a properties file, and I need to use the / forward slash in some of my keys.
e.g.
app.module/hdr.key1=value 1
app.module/hdr.key2=value 2
I just have no choice but need to do it that way. Please advise is this achievable and how to do this?
Thanks.
The use of forward slashes will not cause a problem. To understand why, I suggest you read a critique of the syntax used in Java properties that I wrote. In essence, what you need to know is the following:
Leaving aside edge cases (comment lines, blank lines and escape sequences), the syntax of a name=value pair permits almost any character (including forward-slashes) in the name.
The = can actually be any of the following: (1) = (optionally preceded and/or followed by whitespace); (2) : (optionally preceded and/or followed by whitespace); or (3) just whitespace. So, yes name=value is equivalent to name:value and also to name value.
All escape sequences begin with the backslash character. For details of the escape sequences, I suggest you do a Google search for java.util.Properties to find online documentation for that class, and look at the long description of the load(InputStream) method.

Is there any way to find out an argument passed to a jar was quoted?

I'm trying to pass a number of arguments to my Java application, but I would like to parse them by myself using an intelligent parser that doesn't just rely on whitespace to separate the arguments. An example:
/update source=foo func=(bar, foo ,foo,bar)
This all works nicely by converting everything to tokens and then parse those. However, a problem occurs when I add:
path="./foo/bar/foo bar.txt"
(note the double space between foo and bar).
When I use double quotes, the argument is passed as a single string, preserving the double space. The quotation marks are removed though like this:
path=./foo/bar/foo bar.txt
which makes my parser fail. But when I try to use some other character to use as quotes, like ', the parser works fine but then the shell passes the string as two separate strings, separated at the double space, therefore I lose the information that there were two spaces there.
What can I do to pass an argument using double quotes to keep the literal string representation, but also keep the information that the string was quoted, without the user having to type weird constructions like "'string'"? I'm using Java, maybe there is a way to get the entire line of arguments unparsed by the shell? Or just without the quotes being removed?
Btw, I ran this from microsoft command line, haven't tried a unix shell yet, which might even fail on the single quotes from what I read on the interwebs
On the Windows command line (using cmd.exe), you can escape double quotes with \". For example,
java MyApp path=\"./foo/bar/foo bar.txt\"
will result in
args[0] = path="./foo/bar/foo
args[1] = bar.txt"
while
java MyApp path="\"./foo/bar/foo bar.txt\""
will give you
args[0] = path="./foo/bar/foo bar.txt"
Thanks for the help I got, but I already figured it out:
I know the thing that could be quoted doesn't contain brackets, comma's or equals signs, the things that my parser recognizes.
I know that IF something was quoted and it contained spaces, those spaces would still exist within the split argument.
I know that the original string of arguments is split at every region of whitespace, so the final split arguments don't contain spaces, only those in the quoted parts.
Therefore I can assume that if I parse a split argument, that any space in there does not imply a new token has to be generated, therefore it is retained in the final string-token.
I just have to rewrite my tokenizer now to accept an array of arguments instead of the concatenated string I now create from the args array I get passed in my main() method. That way I can differentiate between skipping real whitespace (going into the next element of the array) and quoted whitespace (any other whitespace).

Using Resourcebundle Properties in Javascript

I work on a Java EE web application that uses a combination of Dojo and plain javascript for the front-end.
We've discovered that when ResourceBundle properties are used in javascript, in some cases they end up breaking code.
Specifically, this happens when the properties contain quotes (single and double) & escape sequences (\n, \s ...).
The solution seems to be to include extra escape characters. For instance, \n needs to be prepended by one more slash (\\n) when used in a Js alert
to correctly render the line break, and Quotes if not escaped truncate the content prematurely for obvious reasons.
Our solution to the above issues so far has been to put in the extra escape characters in the property files itself. But this is something that we would like to move away from.
It seems like this might be a widespread problem and I'd like to hear from the experts on how you might have solved this problem.
Current Usage: key=A newline is represented with \\n and this \" is within quotes \".
Envisioned Usage : key=A newline is represented with \n and this " is within quotes ".
PS: We typically use the <fmt:message> tag to access these values in the front end and for use in javascript.
Consider using StringUtils. If has a method to escape input like yours.
http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)

Java Properties, getting file path

logpath = LoggerUtils.getProperties().getProperty("log.path");
System.out.println("logpath: " + logpath);
The above code returns:
logpath: C:UsersMauriceDesktopLogs
In the properties file is:
log.path C:\Users\Maurice\Desktop\Logs
How do I retain the file separators? I want this to work on Linux as well and not just Windows.
Actually, you need to put this in the property file:
log.path C:\\Users\\Maurice\\Desktop\\Logs
See this:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
more precisely the load method:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
Scroll down a bit and you will see this among other things:
The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.
Backslash \ is an escape character that is silently dropped otherwise.
In a property file, you need to either use forward slashes:
C:/Users/Maurice/Desktop/Logs
Or, escaped backslashes:
C:\\Users\\Maurice\\Desktop\\Logs
You need to escape the slashes as they are special characters. See: Java Properties backslash
The Java properties file format dictates that the backslash character ("\") escapes the character that follow it, so to get a literal windows path you must have:
logpath: C:\\Users\\Maurice\\Desktop\\Logs
However, Java will convert path separator characters for you automatically to suit the runtime platform, so you can avoid this nuisance by always using forward slashes:
logpath: C:/Users/Maurice/Desktop/Logs
You can store the Properties to file first, then load it again to use. Properties will take care of escaping/ unescaping anything.

How can I represent URL (possibly including query string) as a filename in Java without obscuring the original URL?

Is there any real way to represent a URL (which more than likely will also have a query string) as a filename in Java without obscuring the original URL completely?
My first approach was to simply escape invalid characters with arbitrary replacements (for example, replacing "/" with "_", etc).
The problem is, as in the example of replacing with underscores is that a URL such as "app/my_app" would become "app_my_app" thus obscuring the original URL completely.
I have also attempted to encode all the special characters, however again, seeing crazy %3e %20 etc is really not clear.
Thank you for any suggestions.
Well, you should know what you want here, exactly. Keep in mind that the restrictions on file names vary between systems. On a Unix system you probably only need to escape the virgule somehow, whereas on Windows you need to take care of the colon and the question mark as well.
I guess, the safest thing would be to encode anything that could potentially clash (everything non-alphanumeric would be a good candidate, although you migth adapt this to the platform) with percent-encoding. It's still somewhat readable and you're guaranteed to get the original URL back.
Why? URL-encoding is already defined in an RFC: there's not much point in reinventing it. Basically you must have an escape character such as %, otherwise you can't tell whether a character represents itself or an escape. E.g. in your example app_my_app could represent app/my/app. You therefore also need a double-escape convention so you can represent the escape character itself. It is not simple.

Categories