This question is answered.


Permlink Replies: 4 - Pages: 1 - Last Post: 4 Jan 23, 16:11 Last Post By: davidekholm Threads: [ Previous | Next ]
AndreWolff

Posts: 2,268
Registered: 14-Dec-2007
How to get the first gpx-file in a folder output directory?
Posted: 30 Dec 22, 09:56
 
  Click to reply to this thread Reply
For an album with folders I need a function which returns the name of the first gpx-file in the output directory of the folder. It should return an empty string if the corresponding output folder contains no gpx-file.

Who can give me the java code of such a function?
davidekholm

Posts: 4,183
Registered: 18-Oct-2002
Re: How to get the first gpx-file in a folder output directory?
Posted: 30 Dec 22, 16:28   in response to: AndreWolff in response to: AndreWolff
Correct
  Click to reply to this thread Reply
There are many ways to do this, here's one:
/**
 * Find first occurence of file of certain type within specified folder
 * @return first file found or throws NoSuchElementException
 */
File findFirstOfType(File folder, String ext) throws NoSuchElementException {
return Arrays.asList(rootOutputDirectory.listFiles())
	.stream()
	.filter(f -> f.getName().endsWith("." + ext))
	.findFirst().get();
}
 
// Usage example
findFirstOfType(rootOutputDirectory, "zip");
AndreWolff

Posts: 2,268
Registered: 14-Dec-2007
Re: How to get the first gpx-file in a folder output directory?
Posted: 30 Dec 22, 21:23   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thanks David, that saves me a lot of work!
AndreWolff

Posts: 2,268
Registered: 14-Dec-2007
Re: How to get the first gpx-file in a folder output directory?
Posted: 2 Jan 23, 17:42   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
I did ask chatbot https://chat.openai.com/ whether this was correct JAVA code.
This was the answer:

Yes, the code looks correct. It takes in a File object representing a folder and a string representing a file extension, and returns the first file in the folder that has the specified extension. If no such file is found, it will throw a NoSuchElementException.

Here's a breakdown of what the code does:

It converts the list of files in the folder to a stream using Arrays.asList(folder.listFiles()).stream().
It filters the stream to only include files that have the specified extension using .filter(f -> f.getName().endsWith("." + ext)).
It returns the first element in the stream using findFirst().get().
If the stream is empty (i.e., there are no files in the folder with the specified extension), findFirst().get() will throw a NoSuchElementException.
davidekholm

Posts: 4,183
Registered: 18-Oct-2002
Re: How to get the first gpx-file in a folder output directory?
Posted: 4 Jan 23, 16:11   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Cool service!
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums