My problem is when I print the files within the directory, it prints out stuff like 'thumbs.db' and 'desktop.ini'. How to i make it print the name itself. All the files are .png by the way
static File overlayPath1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\" + filesList[0].getName());
thumbs.db and desktop.ini are both files in this directory, but normally in Windows they're hidden. However, because they're still there, they'll show up in your fileList.
If you don't want to use these files, you're going to have to skip them somehow. The implementation I'd suggest is to convert it to an ArrayList, then remove elements that don't match the .png extension.
However, without knowing more about your implementation, though, I can't easily suggest a way to do this.
Related
I know about the Methods in FileUtils, but the FileUtils won't overwrite.
Also in Files.move("", "", StandardCopyOption.REPLACE_EXISTING) when the file exists and it's not empty, then the following exception is thrown: java.nio.file.DirectoryNotEmptyException
Sample:
/file1/test1
/file2/test1
How do I merge, move, and overwrite (such as move in windows), /file1/test1 to /file2/test1 ?
has java function for this job? I want don't write any method
You could do naive approach with standard java util functions, and nit with Files.
First, write a method, what will move and overwrite one file. Before moving, check if the file with this name is is the target folder and delete it, what is simpler, (or open end overwrite content, what is unnecessary complicated).
Then, get the list of all files in source folder and apply the method above in the loop.
image.jpg
There is a load of lines like this with different pictures.
What i need to do is to make a script of some sort that would allow displaying those images without the need to write anything new in the body (it has to find the files from href's and display them ... without causing any more work for a person who puts those pictures there) and without the need to reorganize files (those files are tied to many other things ... change in the directory = everything crashes)
but i cant just find much ... most of the scripts i find requires me to place files in a specific folder or even worse ... to make img src tags for them
Can anyone point me towards some solution here ?
I'm not exactly clear on the question, but if I decipher it correctly a possible solution is to user scandir to read the contents of a directory. Likely will need to modify inside the foreach loop to fit your design (not sure if you have embedded script or not) but this will dynamically fetch and display images inside a directory.
if ($images = scandir('path_to_your_image_directory')) {
foreach ($images as $image) {
print '$image.jpg'; // Might need to preface the $image variable with path to your image directory
}
}
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).
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.
I have a file I need to rename to that of an existing file. This is a copy, modify, replace original operation on an existing JAR file. I've got the first two steps done, I just need help with the replace original bit. What's the best way to rename the new version of the JAR to that of the old. The old JAR doesn't need preserving and I don't want to have a copy of the new with its initial name sticking around.
I have commons lang and io already, so if there's a method I've missed, that would be great.
Java.io.File.renameTo(java.io.File)
You might need to call File.delete() first on the original file first - some systems won't rename a file onto an existing file.
You're going to need to create two java.io.File objects: one for the new file, one for the old file.
Lets call these oldFile and newFile.
oldFile.delete()
newFile.renameTo(oldFile);
Edit: mmyers beat me to it.
This should get you reasonably close:
public boolean replaceOldJar(String originalJarPath, java.io.File newJar) {
java.io.File originalJar = new java.io.File(originalJarPath);
if (!originalJar.isFile()) {
return false;
}
boolean deleteOldJarSucceeded = originalJar.delete();
if (!deleteOldJarSucceeded) {
return false;
}
newJar.renameTo(originalJar);
return originalJar.exists();
}
Is there a problem with deleting the old one and renaming the new one?
I'm not completely sure if you are asking simply how to rename the file in the filesystem or if you also want to reload this new version of your jar?
The rename part sounds easy... just use File.renameTo... unfortunately, there are many platform specific problems related to doing this. Some platforms will not allow you to overwrite an existing file, others will not allow you to rename a file so it changes location onto another partition. If you want to make the process completely safe, you need to do the process yourself by removing the old file first and then renaming the new (or copying if a partition change is possible). This is naturally prone to problems if your application/machine crashes while doing this, since it is no longer an atomic operation. You will thus need to add a check to your applications startup process that looks for rename operations that were in the middle when the crash occured. If you are just updating a single file in this way, it should be pretty easy.
However, if you actually want to reload the jar, there are a few more issues to thing about, but you would need to give a bit more detailed view of the situation to get proper advice on how to do it.