Results 1 to 6 of 6

Thread: Simulating a FileFilter

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Simulating a FileFilter

    Hello,
    I develop a class file, I begin with java and I want to simulate a filter with this function, but I stuck on how to return the collection of File
    Here is my code
    Code:
    Public File[] list(String monFiltre){
            File[] tbfh;
            for(File f: myFile.listFiles()){
            if(f.getName().endsWith(monFiltre)){
                tbfh = f;
            }
            }
            return tbfh;
        }
    Any help on this will be appreciated, thanks for the help.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Simulating a FileFilter

    Hello,
    I think tbfh is an array of File while f is a File, you can not do, they are of different type. Prefer to use ArrayList, like this:
    Here is the code showing this
    Code:
    ArrayList fl = <File> new ArrayList <File>();
    for(File f: mfile.listFiles()){
            if(f.getName().endsWith(mnfl)){
                fl.add(f);
            }
            }
            return fl.toArray(new File[fl.size()]);

  3. #3
    Join Date
    Apr 2008
    Posts
    264

    Re: Simulating a FileFilter

    Hello,
    I am new to java, just a try and nothing serious, I think your class should look something like this
    Code:
    import java.io. *;
    import javax.swing.JOptionPane;
     
     
    Public class File {
        private File mfl;
    
    Public File[] list(String mnfl){
    Try this type of a syntax in your code.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Simulating a FileFilter

    Hello,
    I advice you to add a second parameter to your method 'list' type File to what is more easily reusable. This would:
    Code:
    Public File[] list(mnfl String, File mnfl){
    ArrayList fl = <File> new ArrayList <File>();
    for(File f: mnfl.listFiles()){
            if(f.getName().endsWith(monFiltre)){
                fl.add(f);
            }
            }
            return fl.toArray(new File[fl.size()]);
    }
    But if you do not plan to use this method outside of your class, then forget what I just said. The code works?

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Simulating a FileFilter

    Hello,
    A better idea is of having inheritance. Your class MyFile everything looks like a utility class which you will serve in bcp class. So I think you said the static, makes you a sales person for use in your class. Instead of using inheritance, it would be possible to use the decorator pattern bcp more elegant and does the job. Java can also invoke the power of regex you can use instead of str.endsWith (mnfl). Your filter is a regex and the method is gaining in power and time. Hope the information will help you.

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Simulating a FileFilter

    Hello,
    Alternatively you can try the following code
    Code:
    ..
    Public void Copy(String fileName)throws IOException{
       FileInputStream f =new FileInputStream(this.fileName);
       FileOutputStream fout =new FileOutputStream(fileName);
       FileChannel flch = f.getChannel();
       FileChannel chflout = fout.getChannel();
       long sz = Chin.sz();
       Chin.transferTo(0, sz, chflout);
       Chin.close();
       chflout.close();
       did.close();
       fout.close();
    }

Similar Threads

  1. Error while Simulating TORA
    By amithkhandakar in forum Networking & Security
    Replies: 1
    Last Post: 03-05-2012, 02:07 AM
  2. wormhole attack and simulating blackhole attack in ns-2
    By mANICKAVASAN in forum Networking & Security
    Replies: 9
    Last Post: 02-02-2012, 12:16 AM
  3. Simulating Touch or Flick Gestures in windows phone 7
    By HangDown in forum Portable Devices
    Replies: 2
    Last Post: 31-10-2011, 03:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,006,530.03787 seconds with 17 queries