I need to hold in memory all absolute paths of file names under a given directory.
myDirectory.list() - retrieves String[] of file names only (without their absolute paths).
Don't want to use File Object since it consumes more memory.
Last thing - I can use apache collections etc. (but didn't find anything useful for that).
String directory = <your_directory>;
File[] files = new File(directory).listFiles();
for(File file : files){
if(file.isFile()){
System.out.println(file.getAbsolutePath());
}
}
This works, and I gotta say I'm confused when you say you don't wanna use File objects, but whatever works, I guess.
Doesn't myDirectory holds the directory of all those files?
If so, just combine the path in myDirectory with each of the cells in the array myDirectory.list() returns.
Related
Expected:
find the number files in the folder, and collect the absolute path for each file in the directory.
File certificatePath = new File("resources/NPL");
String absolutePath = certificatePath.getAbsolutePath();
File directory = new File(absolutePath);
int fileCount=directory.list().length;
From the above code getting the no of files in folder (resources/NPL), now i'm struggle to get the absolute path for the files.
You should use listFiles instead fo list() (provided you have the security rights to do so), otherwise you are stuck with the file names.
Well first of all what does it mean to collect it? If you need the file names it is already there for you in an array of String[] which you get from directory.list() method. If you want the full names you can use listFiles() method which returns a File[] and each file has getAbsolutePath() method. Something like:
for (File file : directory.listFiles()) {
System.out.println(file.getAbsolutePath());
}
I am making a JTree that loads files and folder of a FTP Server, using (Apache Commons).
I use this method to load files of a specific directory:
FTPFile[] innerFiles = ftp.listFiles();
I noticed that for any directory, innerFiles [0] is . and innerFiles [1] is ..
It is easy to ignore them by start looking from innerFiles[2], but I just want to know what are these reserved items for and would it make any problem in case of ignoring them?
Those files represent the current directory (.) and the directory above it (..). You should ignore these when creating a tree structure showing all the files and directories.
You could specify a FTPFileFilter that strips these out.
You can just ignore them. Here is an implementation of listing all the actual files, disregarding the two dot ones:
ftp.listFiles(remoteFilePath, file ->
{
val filePath = file.getName();
return !(filePath.equals(".") || filePath.equals(".."));
});
This seems more proper than starting with index 2.
My java code lists all code files under a directory of file system, and load each file one by one:
File[] files = mDir.listFiles();
for(File f: files) {
System.out.println(f.getPath());
//load code file
System.load(f);
}
The above code logically looks good, but is not suitable for my case.
My case is that I can NOT load them in a loop one by one, because there are dependencies among those code files. I need to load the files in a specific order according to dependencies.
Say, I already know there are following files under the directory mDir which should be load in the following order:
["dFile", "xFile", "aFile", "hFile"]
and I already got the directory instance mDir .
How can I load files with above order efficiently in java?
If you already know which files you are interested in then just load them in the proper order.
If you have to see which files are available first and then load them in the specific order, then use one loop to get the names of the existing files, then process the list by picking the correct files in the correct order.
I'd suggest just setting the working directory correctly (see Changing the current working directory in Java?) and then doing
for(String fname : fileArray) {
System.load(new File(fname));
}
(where fileArray is the list of file names) or
for(String fname : fileArray) {
System.load(new File(mDir.getPath() + fname));
}
if you're intent on loading from a specific directory.
Other than that, you'd need to divine the dependencies from each file in order, or read the list of files to load from some other source (an array, another file, whatever).
I have like 2 files with almost same name like "myfile_1234.mp4" and "myfile_5678.mp4" in the same directory. One File, say, "myfile_1234.mp4" size is ZERO bytes where as the other file say "myfile_5678.mp4" has some size NOT EQUAL to ZERO (say 32kb). Now I want to delete the 1st File from the Directory but not the other File.
Can we have something like a loop temp pointer to the Files in the Directory and we would then check for Size of file and delete it when its size is ZERO.
Can anyone help me here...?
This is a relatively straightforward exercise. Here are the things that you need to know to do understand what to do:
A directory is represented by a File object. Create a File object for the directory that you want to list
The listFiles() call produces an array of all files in the directory
The length() method returns the size of the file
The delete() method deletes the file
Your code will look like this:
// Reference the directory in which the files reside
File dir = new File("c:/my/test/directory");
// Go through the files in the directory in a loop
for ( File file : dir.listFiles()) {
// Make sure that an entry is a file (it could be a directory)
// and that its size is zero
if (file.isFile() && file.length() == 0) {
// If both conditions are true, delete the empty file
file.delete();
}
}
You can make additional conditions to check if a file has a particular name, extension, etc.
You can have a file (handle) created like
File myFileOne = new File(path);
File myFileTwo = new File(anotherPath);
Having such handles it is quite easy to check files size or names and delete the files as well. For more info on Java File see: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File
This question has been brought up before, and I have searched many of the answers. It always ends in "You want getResourceAsStream". This is not what I am looking for.
My issue is , for a game object, I am using a folder structure to keep sprite strips rather than having one large sprite. This results in :
Media/
CharacterName/
AnimationName/
image.extension
the programming object just holds it's folder as a string, and I pass the getResource() URL to an object to fill the map of images. there can be {n} number of AnimationName/ sub directories. My error comes from this code:
dir = new File(s.toURI());
I take the directory, and call listFiles and pass the file names found to the sprite loader. Here is a code snippet:
dir = new File(s.toURI());
File[] chld = dir.listFiles();
//get a list of files in the image/character folder
for(File f:chld)
{
//get a list of the files for each dir
File[] grandChild = f.listFiles();
for(File t:grandChild)
{
String fname = t.getAbsolutePath();
System.out.println(fname);
String temp = fname;
temp = temp.substring(temp.lastIndexOf("/") + 1,temp.lastIndexOf("."));
String animName = temp.replaceAll("\\d*$", "");
int numPics = 0;
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(temp);
while(m.find()){
numPics = Integer.parseInt(m.group());
}
System.out.println("animation name: " + animName);
System.out.println("file name: " + fname);
System.out.println("number of pictures: " + numPics);
Animations.put(animName, sl.loadStripImageArray(fname, numPics));
}
}
Excuse the poor naming and temp variables, it's still being worked on.
sl is the sprite loader, and Animations is a hash map. This works fine until I package the project. I don't want to write a bunch of convoluted code that only works if I have a jar file, and not when I'm working in netbeans with the source folders.
I have considered having an application data folder, but I'd like to stay with a jar package if I can.
You do still want to use getResourceAsStream. Nothing here requires that all resources must be kept at the same folder within the JAR. You can use relative paths, or absolute paths to the root of the JAR by prefixing your path with /.
You can't make File work with resources within the JAR - even if instantiated with a URL that points to a resource contained within a JAR.
You may have to rework some other things, as the classpath is not really meant to be enumerated against (as you're currently listing files from the parent directory). It is designed to retrieve a resource by name. So one possibility (that I would recommend) is to have a "manifest" file that contains the files you want to load from each directory. (Read this file, then load the additional resources by name.)
Alternatively, if you can find the name of the JAR file you're loading from, you can create a Jarfile from it, then call its entries() method to find all of the contained resources. But even then, they aren't returned in a "tree structure", so ideally, you'd read this one, create your own tree structure from it (possibly as a series of Maps), then use it to retrieve the "directory listings" as needed.
If you are absolutely sure that the sprites are located in a jar - you could try using the JarFile class. There is a method entries. I didn't try it but it seems to return all resources located in the whole jar file. You would have to filter out which resources are in the right path.