RandomAccessFile - System cannot find path specificied - java

Good day,
I've been working on a program for calculating and also saving trainings and calories used between them and I've hit rock bottom.
This is my RandomAccessFile:
RandomAccessFile dat = new RandomAccessFile("training\\"+lastTraining+".dat","rw");
It doesn't work. It returns me "The system cannot find path specified."
Now, I don't have folder neither file created, because I want program to create them. What am I doing wrong? I already tried all kind of slashes in path but nothing works.
Thanks!

Have you tried passing in the full path?
E.g: instead of "training\file.dat" do
(Windows) "C:/path/to/file/file.dat"
(Unix) "/path/to/file/file.dat"
Edit: Note that the folder(s) you are trying to place the file in have to exist beforehand, as RandomAccessFile will not create them and throw the FileNotFoundException

Related

FileNotFoundException (The system cannot find the path specified) even though I already edited it

I am encountering this error
java.io.FileNotFoundException: C:\Users\user\Desktop\OUTPUTS\REPORT.xlsx (The system cannot find the path specified)" even though I already edited the directory/path of the file to " fileOut = new FileOutputStream("C:\Users\samuel\Desktop\REPORT.xlsx")
C:\Users\user\Desktop\OUTPUTS\REPORT.xlsx is the previous directory.
I edited it because it is a directory of another computer.
Please help me! Thank you very much.
in java the (\) character is used as an escape character. therefore you must use 2 together.
i.e.
C:\\Users\\user\\Desktop\\OUTPUTS\\REPORT.xlsx
You have tried almost most possible way, there might be problem with file then.
1) check filename is properly spelled without any space.
2) check whether filename is directory.
Refer This Stack answer for more info

Confusion on – File f = new File(....); if(f.exists()) {…}

In most other languages / operating system I've worked with, a statement like
File f = new File(....);
would attempt to open the file, and either create one if it didn't exist, or return an error code if it was missing. So, what happens in java.io? I'd like to understand the mindset of the run time engine. Can I actually get a handle to a non-existent file? Dose the java run time engine hold off on making the file until the 1st time I write to it? If not, shouldn't
if(f.exists()) {…}
always be “true”?
- any comments welcome - Joe
As described in the Javadoc, java.io.File represents a path, not a file. Bad naming. Think of operations on File as path operations, because that's what they are. Unless something specifically says that it creates a file at a given path, it doesn't.
You can create a File by creating a reference to a non existing File. If you pass that File to a FileOutputsteam the Stream will create the File on your drive.
So if you cant be certain that the File doesnt exist you need to be able to check its existence with f.exists() othwerwise you wouldnt be able to make an intelligent decision on how to proceed
From the Android Developers official documentation you can read that a File is:
An "abstract" representation of a file system entity identified by a
pathname.
The actual file referenced by a File may or may not exist. It may
also, despite the name File, be a directory or other non-regular file.

java file.renameTo() does rename file but returns false. Why?

The problem is that I need the file to move before the rest of my logic will work so when the method returns false I stop execution.
However, when I check on the file in windows explorer it has a new name and it moved.
Just curious why this is happening.
here is some sample code I just tried to recreate the issue. It's pretty much the same thing and it's working fine.
File testfile = new File("TestFile");
if(!testfile.exists()){
testfile.mkdirs();
}
File sample = new File("sample.txt");
if(sample.exists()){
boolean success = sample.renameTo(new File(testfile.getPath() + "\\" + sample.getName()));
if(success){
System.out.println("Moved");
}
else{
System.out.println("Failed");
}
}
Edit: Solved it. I'm sorry for wasting everyone's time with something so silly. However, I really dont think I would have tracked this down if not for making this post.
The solution was that I was actually looping through several files to move. When the output said it failed then the program stopped and when I looked in explorer only the first of the files was actually moved so I assumed it was moving and then returning false. However, the issue was that I was using the wrong variable as an index and so what was happeneing was that it did successfully move the file in index 0 and then when the loop repeated the index didnt increment so it tried to move index 0 again and therefore failed.
Like I said, very stupid but thanks for bearing with me.
Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says:
Many aspects of the behavior of this method are inherently
platform-dependent: The rename operation might not be able to move a
file from one filesystem to another, it might not be atomic, and it
might not succeed if a file with the destination abstract pathname
already exists. The return value should always be checked to make sure
that the rename operation was successful.
You can use apache.commons.io library, which includes FileUtils.moveFile() or also the Files.move() method in JDK 7.
Isn't it possible that you file has a Inputstream open somewhere but has not been closed and so the rename is not working. Try closing all open streams relevant to the file object before closing.
This one worked for me
File file = new File("E:/Javadocs/" , "new.txt");
File file1 = new File("E:/Javadocs/" , "myDoc.txt");
file1.createNewFile();
if (file1.exists()){
System.out.println(file1.renameTo(file));
}
This will create a file myDoc.txt and rename it to new.txt and will print true
I've also tried with File(URI) constructor it worked fine

Java renameTo Files Showed 0 File Size then were gone...Windows still holds the space though

So I have been making a program in Java to organize all my files. I am starting with movie files since that is what I have the most of, but I plan on being able to checkBox all file types eventually.
Anyway, I had renameTo working fine when moving all files to a new folder. Next step was trying to alphabetize the files. I got this working fine as well, but obviously I needed to be able to remove articles(i.e. "the", "a", "an") in order to properly alphabetize. I was able to do this in my ArrayList pretty easily, and then decided to actually rename the files.
I did a renameTo in order to rename the file the same filename minus the articles. I used:
File newb = new File(folder+""+filelist[i].getName().substring(4));
if(filelist[i].renameTo(newb)){}
else{
System.out.println("FAILED");
}
Where folder is obviously the folder location the file is in and it is a substring of the original string minus 4 characters at the beginning for the word "The_" or "The."
It then attempts to rename the file from within the same folder to itself minus the substring.
The result of this code is the file disappearing and file size being listed as 0 when calling the length() of that file. The file is not findable in Windows Explorer, BUT there is no extra space on my HDD!
Now to the questions:
What is wrong with this? Should I be putting a temp file into another folder or something?
Is there a way to release the files from Windows so they are not forever clogging up space on my HDD?
Anything else you need to know? Want to add?
I'm not sure what's going on, but constructing file names by appending strings can cause problems. (In particular, if folder doesn't end with the path separator character, you might find your moved files in the parent folder. That would explain why the files disappeared without freeing any disk space. So instead of renaming, say, C:\movies\The_Sting.avi to C:\movies\Sting.avi, it ended up as C:\moviesSting.avi.)
Try this instead:
File parent = filelist[i].getParentFile();
String newName = filelist[i].getName().substring(4);
if (!filelist[i].renameTo(new File(parent, newName)) {
System.out.println("FAILED");
}
Obviously, if all the files in filelist are from the same folder, then you only have to assign to parent once (before you start looping).

Java - Unable to Detect Files

Following on from my previous question, my program doesn't detect the 300 images that have just been created in a particular directory; instead, it only detects desktop.ini, which is not the case as I can physically see that the files have been created within said directory and do exist.
Can somebody please explain why this happens as when I run the program the next time, it seems to work just fine?
The only way that something is detected within the directory on the first run is when there is at least one file which exists in the directory before the program is compiled and executed.
Many thanks.
UPDATE: Files are detected as follows:
//Default greyscale image directory (to convert from greyscale to binary).
static File dirGrey = new File("test_images\\Greyscale");
//Array of greyscale image filenames.
static File imgListGrey[] = dirGrey.listFiles();
without knowing how you create the images, this question is akin to 'How many kittens are under my desk right now?'
Are you creating the files yourself? If so, are you closing any file handles referring to those files once they are created?
You're creating the file list in a static array, and it's created when the class containing the array is loaded by the Java class loader, which is probably before you create the image files. That's why the array contains an outdated list.
static is rarely needed, mostly useful for constants (things that never change, such as 42), for pure functions (Math.sqrt()) and a few other special cases. When you use it, you have to learn all the tricky initialization order stuff. Otherwise, just stick with non-static variables.

Categories

Resources