Detect the file my JavaFX program was opened with - java

After making an installer to my app, I can make it associate with a file extension, which is great, but I can't figure out how can I detect the path of the file that my program was opened with.
Any help is appreciated!

For anyone having this problem, the file path is the first argument in getParameters().getRaw()
Javadoc description of the function.
Retrieves a read-only list of the raw arguments. This list may be empty, but is never null. In the case of a standalone application, it is the ordered list of arguments specified on the command line. For named parameters, each <name,value> pair is represented as a single argument of the form: "--name=value".
Thanks, James_D!

Related

How to properly call textfile in javaprogram on mac?

In my assignment, a series of text files are needed in order for a picross game to occur that the user can play. While I have all the code properly set and done, when i try and path the folder within my assignment that contains all the textfiles for the game, it tells me that what've i provided is a string not path and won't accept my answer. Is there any other way I could input the textfiles on mac rather then through the pathname since it does not work?
Assuming that it is referring to the java.nio.file.Path Interface, you can look at the documentation here, along with the documentation for java.nio.file.Paths here
I expect something like this should work:
Path path = Paths.get("/your/path/here");

Is there a way to write the Eclipse filesearch (Ctrl + H) in code?

I am currently trying to search the whole project for different strings. The best way to do this manually is the built-in Eclipse function "fileSearch". Is there a way to write a method in Java that does exactly the same thing so it searches every file for a given string and then returns true or false when it finds something?
Thanks for your help,
Yes, that is possible. You can implement a tree walker as explained in the Java Tutorial. For every file encountered, the content of the file would be read and checked for an occurrence of the searched for string.
Depending on the result you want to achieve, you could stop the tree walk as soon as at least one matching file is found, or you could collect the paths of all matching files, or ...

Why can't I find the file with normalized path on windows system?

I want to check if a file exists by its filepath using java. I know there is a method in Paths class called normalized(). Here is my code:
File file = Paths.get(fileName).normalize().toFile();
if (!file.exists()) {
return "File does not exist.";
}
The filename is a file path with sysmbols of current directory like "."or".."
After the path is normalized, it just delete the dots of the path and windows can't find the new path with out dots. But the code above works fine on other systems. I wonder why and what should I do if I want to check if a path with dots exists or not?
Why are switching from the Paths API to the file API?
use: Files.exists(Paths.get(fileName).normalize());
As the docs says, normalize removes /./ constructs, as well as X/../ constructs. This can nevertheless result in slightly different intended files in case of weird softlink constructions, which, yes, even on windows, is a thing you can do. It may be related to that. Give us the path pre- and post normalization (just sysout it) and we can perhaps give you some more details on this.
Generally you don't want normalize. Depending on situation you either just want the path as is, or, (in case you need to store it for later, check it against certain filters, or render it to the user), path.toAbsolutePath().

Getting a Path value from a JFileChooser in Windows XP

As the question states, I am trying to get a Path value, using JFileChooser. The returns from JFileChooser present me with two problems. First, the exact nature of what I am doing involves letting the user choose a directory, then, renaming all files of a certain type, for instance, all .pdf files, within that folder while leaving all other files alone. The problems I am having with JFileChooser in this regard are two-fold:
1) I have figured out how to let JFileChooser show the files inside a directory, but not allow the user to choose a file specifically. However, the return value of the string does not include the currently viewed directory inside JFileChooser when "Open" is pushed. For instance, if a user navigates to C:\Documents and Settings\myFolder\Documents\Work and sees the contents of the Work directory, then presses "Open", the String returned as the getAbsolutePath() call is "C:\Documents and Setting\myFolder\Documents" the "Work" is never returned. How do I get that to be presented in the final outcome?
2) Once I have that String, how do I convert that to a Path so that I can operate on the files within it? The "\" is Java's escape, and to use the file path as a valid Path value it needs to be escaped, i.e. "C:\Documents and Settings\myFolder" doesn't work, it has to be "C:\Documents and Settings\myFolder". How do I get from the single slash () return String from JFileChooser to the double slash (\) Path I need?
I know that the answer is likely simple, I am just more used to dealing with Linux, where this is not a problem. I have just landed my first job using my programming skills, and of course, everything is Windows.
And before someone suggests, yes, I know using "/" would work for relative paths, but for the application I am writing, I need to use absolute paths as these files could be on a host machine, or a network drive, so I can't use relative naming, unless I am overlooking something due to not having a whole lot of network programming experience.
I thank you all for your time in reading, if not responding to this.
Tommie Matherne
1. For getting the whole path of the selected directory do this:
Here myJfc is an instance of JFileChoooser here
String path = myJfc.getSelectedFile().getAbsolutePath().toString();
2. Conver the String into a path using this:
Use "\\" instead of "\" in the path.
ie. C:\\Documents and Settings\\myFolder
Please see that you have proper Privilege in the C: drive else your file will not be created there, if you Do Not have the privilege to create a file in C: drive, try some other drive which you have like D:, E: etc.. You wont have any Privilege problem there.
File f = new File(path);

Saving a video file by increamenting file name in a loop

I want to save a video file in C:\ by incrementing the file name e.g. video001.avi video002.avi video003.avi etc. i want to do this in java. The program is on
Problem in java programming on windows7 (working well in windows xp)
How do i increment the file name so that it saves without replacing the older file.
Using the File.createNewFile() you can atomically create a file and determine whether or not the current thread indeed created the file, the return value of that method is a boolean that will let you know whether or not a new file was created or not. Simply checking whether or not a file exists before you create it will help but will not guarantee that when you create and write to the file that the current thread created it.
You have two options:
just increment a counter, and rely on the fact that you're the only running process writing these files (and none exist already). So you don't need to check for clashes. This is (obviously) prone to error.
Use the File object (or Apache Commons FileUtils) to get the list of files, then increment a counter and determine if the corresponding file exists. If not, then write to it and exit. This is a brute force approach, but unless you're writing thousands of files, is quite acceptable performance-wise.

Categories