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);
Related
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");
I have been working on this method to open a file and read its contents into an ArrayList. For some reason, it is storing the file path in the ArrayList and not the file contents!
text = new MyArrayList<>();
try (Scanner s = new Scanner(new File(fileName).getAbsolutePath())) {
while (s.hasNext()) {
text.add(s.next());
}
}
Could you help me figure out where I am making the mistake, thank you!
P.S. MyArrayList is my own implementation of the ArrayList, it works as a ArrayList.
When we work with files in java, we have to be very specific about the file we are talking about.
There could be more than one files with the same name in your entire file structure, how would java decide which file it needs to consider. We have below two options
Working with absolute path - First option is to provide the absolute path to Java program, so that it can simply create an object using that path.
Working with relative path - When we are working with relative path, java only searches the file in the classpath, which is by default current working directory, that is, your projects root folder.
In case you want to use the relative path, make sure it lies in the classpath.
Otherwise make sure to provide the absolute path to the program.
Giving any filename to the program and expecting java to find out the absolute file path is not the right.
Needless to say that your Scanner Object has to be correct.
In my Java application, the user can give the absolute path of a file (that he wants the application to read and do stuff with) through some configuration file.
This path is stored in a String, and I have a method like
public void readFile(String filePath){
// read the file using its absolute path then do stuff
}
My point is : I don't know if a user will give me a path like
C:/my/path/myfile.ext
or like
C:\my\path\myfile.ext
(assuming it's a Windows user).
Is there a consistent way to deal with it?
or should I force users to choose one or the other way? and in this case: which one is best?
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().
To make my question more specific:
mapFile = new File("D:/Documents/Graphics/map.png");
map = ImageIO.read(mapFile);
'map' is a BufferedImage I wish to put read image into. I have to use a bit uncomfortable directory format, an URL leading simply to project's src folder would be most welcomed (path would be "map.png" only). I do have a simple method that uses URL's and it's using something like this to get image adress:
java.net.URL imgURL = SpineMain.class.getResource("map.png");
but that beauty does not meet File's expectations (String). Is there any relatively simple way to change it?[Btw - what I want to achieve is easily packable and movable folder with classes and essential files alike. Could any of you provide me with some clue how to start messing with exe files, it's subfolders with content files and so on? I know this does have little in common with my original question and is maybe too general, but I would be most grateful. Thanks for taking your time helping :) ]
Straightforward method: move your 'Graphics' folder into the one containing your main program, and add "Graphics/" before the image name in your getResource function.
Usually, a path that does not start with a drive and/or (back)slash, is considered being offset by your current path; so just "Graphics/map.png" ought to be enough to locate it.
This depends on what Java engines consider "the current working directory"; it should be the one your jar file is executing from. If so, you can use it as a fixed point in the path specification, and tag on any sub-folder names at will.