Java FileFinder using Breadth First Search algorithm fails

  • Thread starter Thread starter Sam Groves
  • Start date Start date
AI Thread Summary
The discussion revolves around the development of a file-finding application using Java, specifically focusing on a method called `calc()` that employs the Breadth First Search algorithm to locate files based on a specified relative path. The user encounters an OutOfMemoryError when executing the method, which prints the root directory but fails to find the expected files. This issue is suspected to stem from an infinite loop, likely caused by the way directories are being traversed, particularly with dot (.) and double dot (..) directory references. Suggestions for troubleshooting include using a debugger to step through the code and printing the state of the queue during execution to identify where the loop may be occurring. Additionally, there is a recommendation to rename the `calc()` function to better reflect its purpose, which could enhance code readability and maintainability.
Sam Groves
Messages
11
Reaction score
0
I am developing a app which would be like the File Explorer except the fact that it will have many more features to it.

I am trying to implement a function
Code:
 void calc()
which would try to find a file with a specific relative path.I am using the Breadth First Search algorithm for my blind search.

I have this Java Class:

[CODE lang="java" title="FileFinder"]import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
public class FileFinder
{
String searchPath;
String diskLocation;
String[]locations;
public FileFinder(String searchPath,String diskLocation)
{
this.searchPath = searchPath;//specificy search path
this.diskLocation = diskLocation;//specify disk location
locations = new String[33];
}

public void calc() throws IOException {
int x = 0;
ArrayDeque<String>searchedFiles = new ArrayDeque<>();
searchedFiles.add(diskLocation);
while(!searchedFiles.isEmpty())//if queue is empty we have carried out our search
{
for(String i:searchedFiles)
{
System.out.println(i);
File f = new File(i);
try{
var files = f.listFiles();//if the java file is a directory
while(files!=null) {
for (File fl : files) {
searchedFiles.addLast(fl.getCanonicalPath());//add every Java file of the directory to the queue
}
}
}
catch(Exception e)
{
}
if(f.isFile())if the Java file is a file
{
if(f.getPath()==searchPath)if relative path -> search path
{
if(x< locations.length-1)check if there is space in the array to not overwrite strings/be out of bounds
{
locations[x] = f.getCanonicalPath();//add canonical path to the array of results
x++;//increment x
}
}
}
searchedFiles.remove(i);//remove the visited Java file
}
}
printFindings();
}
public void printFindings()
{
for(String i:locations)
{
System.out.println(i);
}
}
}[/CODE]

I am inserting my USB Flash Drive which has few directories and files in total(10 or 11?)
But when I call it in main it prints out this message:

Java:
D:\
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.util.Arrays.copyOf(Arrays.java:3720)
    at java.base/java.util.Arrays.copyOf(Arrays.java:3689)
    at java.base/java.util.ArrayDeque.grow(ArrayDeque.java:150)
    at java.base/java.util.ArrayDeque.addLast(ArrayDeque.java:308)
    at FileFinder.calc(FileFinder.java:54)
    at Main.main(Main.java:11)

The searchPath parameter is HelloWorld.txt , I have created one file at the D:\ directory and one at the D:\as\ directory so I expected the canonical paths of those 2 files to be shown in the output however I get this weird message.

As you see D:\ is printed out correctly but then why it throws a OutOfMemory exception?I suspect there is a endless loop but cant seem to figure out where it is.
 
Technology news on Phys.org
My suggestion would be to use a debugger and step through the code as it executes.

Alternatively, you could print the isEmpty flag set as you loop and print out when you add or drop an entry. It should become obvious where you went wrong.

one possible trip up could be searching over a dot or double dot directory Ie
”,” is the current directory and “..” is the parent directory.
 
In addition to what @jedishrfu said about using a debugger, you should come up with a better name for the function you're trying to write.
Sam Groves said:
I am trying to implement a function void calc() which would try to find a file with a specific relative path.
Coming up with a better name would help readers (including yourself in a few weeks) understand what the function is supposed to do. Your description of what this function should do might give you an idea for a better name.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top