List of files starting with a particular letter in java

List of files starting with a particular letter in java



I've got some files in the relative directory (directory the app is running in) starting with '@' and I need to open all of them in java. Show me a way to accomplish it. If it helps, I'm working on netbeans.They're basically .ser files. So I have to fetch the objects in them






What does "open all of them in java" mean? Do you want to open them by launching their associated applications, or do you want to read the bytes from these files?

– Bart Kiers
Apr 15 '11 at 20:26






THey're basically .ser files. So I have to fetch the objects in them.

– Mojo_Jojo
Apr 15 '11 at 20:28




5 Answers
5


File dir = new File(".");
if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
for(File file : dir.listFiles())
if(file.getName().startsWith("@"))
process(file);



After revisiting this, it turns out there's something else you can do. Notice the file filter I used.


import java.io.File;
class Test
public static void main(String args)
File dir = new File(".");
if(!dir.isDirectory()) throw new IllegalStateException("wtf mate?");
for(File file : dir.listFiles(new RegexFileFilter("@*\.ser")))
process(file);



public static void process(File f)
System.out.println(f.getAbsolutePath());




Here's the RegexFileFilter I used


public class RegexFileFilter implements java.io.FileFilter

final java.util.regex.Pattern pattern;

public RegexFileFilter(String regex)
pattern = java.util.regex.Pattern.compile(regex);


public boolean accept(java.io.File f)
return pattern.matcher(f.getName()).find();





And here's the result. Note the three good files and the three bad files. If you had to do this on a more regular basis, I'd recommend using this, especially if you need to do it based on other attributes of the file other than file name, like length, modify date, etc.


C:junkj>dir
Volume in drive C has no label.
Volume Serial Number is 48FA-B715

Directory of C:junkj

02/14/2012 06:16 PM <DIR> .
02/14/2012 06:16 PM <DIR> ..
02/14/2012 06:15 PM 0 @bad.serr
02/14/2012 06:15 PM 0 @badser
02/14/2012 06:15 PM 0 @first.ser
02/14/2012 06:15 PM 0 @second.ser
02/14/2012 06:15 PM 0 @third.ser
02/14/2012 06:15 PM 0 bad@file.ser
02/14/2012 06:24 PM 692 RegexFileFilter.class
02/14/2012 06:24 PM 338 RegexFileFilter.java
02/14/2012 06:24 PM 901 Test.class
02/14/2012 06:24 PM 421 Test.java
10 File(s) 2,352 bytes
2 Dir(s) 10,895,474,688 bytes free

C:junkj>java Test
@first.ser
@second.ser
@third.ser



If it helps check java.io.FileFilter
.


java.io.FileFilter



Yes, open a directory File, get its List of child Files using a FileFilter that only allows through those file names that you want.



You could use java.nio.file.DirectoryStream;


import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

//Java version 7
class A
public static void main(String args) throws Exception

// Example directory on dos system
Path dir = Paths.get("c:\a\b\");

/**
*
* Create a new DirectoryStream for the above path.
*
* List all files within this directory that begin
* with the letters A or B i.e "[AB)]*"
*
*/
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "[AB]*"))

// Print all the files to output stream
for(Path p: stream)

System.out.println(p.getFileName());


catch(Exception e)

System.out.println("problems locating directory");





please see
here, I think that is what you need. See the post referring to FileFilter



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ