java File.separator becomes "%" in path of the File on Windows - java

I try to read files from the resources folder. The problem is, that the File.separator turns into a "%" on Windows.
String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + "filename").getFile());
The inputFilesFolder is still fine (input_files/), but after creating the file file.getPath() becomes D:\blabla\input_files%filename.
Then I try to read the file, but I get a FileNotFoundException (big surprise).
What's wrong here?

File.separator is a file system thing. When you're using classLoader.getResource() always use forward slash as the name of a resource is a '/'-separated path name.
See Javadoc for getResource()

Try this:
File file = new File(classLoader.getResource(inputFilesFolder + filename).toURI());

How about
String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + filename).toString());

Related

File pathing leading to errors

I'm working on a new program that opens different files. The thing is when I change the computer and the files need to be opened they don't because the computer can't find those files. Is there a way to make the files reachable even on another computer? I mean the files are in a folder inside the project but the path changes when you open it on a new computer.
File myFile = new File("C:\\Users\\Usuario\\Documents\\NetBeansProjects\\Info12\\src\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx");
In a Maven Project, all yours files are in src/main/resources so:
src/main/resources/info12/Dokumente/Aufgaben/Bubblesort.docx
Your java code is:
File myFile = new File("src\\main\\resources\\info12\\Dokumente\\Aufgaben\\Bubblesort.docx")
and more smart:
File myFile = new File("src" + File.separator + "main" + File.separator + "resources" + File.separator + "info12" + File.separator + "Dokumente" + File.separator + "Aufgaben" + File.separator + "Bubblesort.docx")

Cant figure out Java find for an "abstract path"

I am working in eclipse trying to create a file.
This is my code:
File file = new File("C:\Users\Local Admin\Desktop\1.txt");
I copied the path I am using directly from the properties of the file but I keep getting the error
Invalid escape sequences.
I tried adding an extra "\" in front of each "\" but that didn't fix anything.
Any suggestions?
It should be either:
File file = new File("C:\\Users\\Local Admin\\Desktop\\1.txt");
Or:
File file = new File("C:/Users/Local Admin/Desktop/1.txt");
Replace "\" by either "\\" or "/".
Try this.
File file=new File("C:/Java/hello.txt");
Try storing the path of the file in a string variable and passing that string variable.Use "\\" if "\" doesn't works.
String filePath="your path";
File file=new File(filePath);

How to find if File exists on internal Storage [duplicate]

I simply cannot find how to get one specified file from the external storage.
I know that with getExternalStoragePublicDirectory(), you get the external storage directory but I can't get further. I need some kind of method where you have to give the name of the file and it returns the file.
Thanx
Better than using File.separator is this, which is standard Java:
final File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), filename);
You can just do this:
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + fileName);
Better still:
final File file = new File(Environment.getExternalStorageDirectory(), filename);
I also use getExternalStoragePublicDirectory() and everything goes well. My filename is "DE Disimpan"
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File filepath = new File(file,"DE Disimpan");

relative file path not working in Java

After reading that is it possible to create a relative filepath name using "../" I tried it out.
I have a relative path for a file set like this:
String dir = ".." + File.separator + "web" + File.separator + "main";
But when I try setting the file with the code below, I get a FileNotFoundException.
File nFile= new File(dir + File.separator + "new.txt");
Why is this?
nFile prints: "C:\dev\app\build\..\web\main"
and
("") file prints "C:\dev\app\build"
According to your outputs, after you enter build you go up 1 time with .. back to app and expect web to be there (in the same level as build). Make sure that the directory C:\dev\app\web\main exists.
You could use exists() to check whether the directory dir exist, if not create it using mkdirs()
Sample code:
File parent = new File(dir);
if(! parent.exists()) {
parents.mkdirs();
}
File nFile = new File(parent, "new.txt");
Note that it is possible that the file denoted by parent may already exist but is not a directory, in witch case it would not be possible to use it a s parent. The above code does not handle this case.
Why wont you take the Env-Varable "user.dir"?
It returns you the path, in which the application was started from.
System.getProperty(user.dir)+File.separator+"main"+File.separator+[and so on]

Java: How to create a new folder in Mac OS X

I want to create a folder in ex. my desktop in Mac OS X
I try to use this code, instead of Mymac is my name of course:)
String path="/Users/Mymac/Desktop";
String house = "My_home";
File file=new File(path);
if(!file.exists())
file.mkdirs(); // or file.mkdir()
file=new File(path + "/" + house);
try {
if(file.createNewFile())
{
}
} catch (IOException ex) {
ex.printStackTrace();
}
Do you know how I could create a new folder?
And another thing is when I want to create a folder in the directory where my code is, do you know how I could write that? I have tried
String path="./";
String path="::MyVolume";
String path=".";
A platform-independent way:
File rootDir = File.listRoots()[0];
File dir = new File(new File(new File(rootDir, "Users"), "Mymac"), "Desktop");
if (!dir.exists()){
dir.mkdirs();
}
Your code is ok and will work. Perhaps you have a typo in your username in your file-path ("Mymac"), so you don't see the changes, since they go to another folder.
Running this code on my machine works fine and gives the expected result.
To make your code platform-independant, you can build your file-path with the following trick:
File path = new File(File.listRoots()[0], "Users" + System.getProperty("file.separator") + "Mymac" + System.getProperty("file.separator") + "Desktop"));
If "My_home" should be a folder and not a file, you have to change the file.createNewFile() - command. More detailed information you'll find in the answer of Thomas.
To find the folder/directory where one of your classes is (assuming they are not in a Jar), and then to create a subfolder there:
String resource = MyClass.class.getName().replace(".", File.separator) + ".class";
URL fileURL = ClassLoader.getSystemClassLoader().getResource(resource);
String path = new File(fileURL.toURI()).getParent();
String mySubFolder = "subFolder";
File newDir = new File(path + File.separator + mySubFolder);
boolean success = newDir.mkdir();
(The code above could be made more compact, I listed it more verbosely to demonstrate all the steps.) Of course, you need to be concerned about permission issues. Make sure that the user which is running java has permission to create a new folder.
Use file.mkdir() or file.mkdirs() instead of file.createNewFile() and it should work, if you have permission to create new folders and files.
mkdirs() will create the subfolders if they don't exist, mkdir() won't.
To create a directory in your base directory (the one you start your application from), just provide a relative path name: new File("mydir").mkdir();
Edit: to make file handling easier, I'd suggest you also have a look at Apache Commons' FilenameUtils and FileUtils.

Categories