Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:21:20+00:00 2026-05-22T18:21:20+00:00

A FileManager Class has a static filed to hold a file collection, this collection

  • 0

A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class contains public method for client code to call such as addFile, addFolder, deleteFile, deleteFolder, these method operate on the collection.
My question is:
What java data structure is best for this case ?
How to create model class for File and Folder ?

some example will be good.

best regars.

// added @ 2011/05/27
thank everybody !
acutaly I am trying to buidl a eclipse-rcp application to manage some jdbc connection profile.
here is my code:

package com.amarsoft.sysconfig.plugin.model;

/**
 * @author ggfan@amarsoft
 *
 */
public class TreeNode {

    /**
     * unique key
     */
    private String key;

    /**
     * used as label in a JFace TreeViewer, 
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }


}

public class LeafNode extends TreeNode {

    private FolderNode parent;

    public void setParent(FolderNode parent) {
        this.parent = parent;
    }

    public TreeNode getParent() {
        return parent;
    }
}

package com.amarsoft.sysconfig.plugin.model;

import java.util.List;

public class FolderNode extends TreeNode {

    private List<TreeNode> children;

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public List<TreeNode> getChildren() {
        return children;
    }
}




package com.amarsoft.sysconfig.plugin.model;

import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

/**
 * 连接配置模型类
 * @author ggfan@amarsoft
 *
 */
public class ConnectionProfile extends LeafNode{

    /**
     * url
     */
    private String url;

    /**
     * JDBC driver id
     */
    private int driver;

    /**
     * user name for logon
     */
    private String user;

    /**
     * password for logon
     */
    private String pswd;

    /**
     * default constructor
     */
    public ConnectionProfile() {

    }

    /**
     * construct a instance using a XML element
     * @param xmlElement the XML element
     */
    public ConnectionProfile(Element xmlElement){
        this.setName(xmlElement.attributeValue("name"));
        this.setUrl(xmlElement.element("url").getTextTrim());
        this.setUser(xmlElement.element("user").getTextTrim());
        this.setPswd(xmlElement.element("password").getTextTrim());
    }

    /**
     * serialize as XML
     * @return
     */
    public Element asXML(){
        Element e = new DefaultElement("profile");
        e.addAttribute("name", this.getName());
        e.addElement("url", escapeNull(this.getUrl()));
        e.addElement("user", escapeNull(this.getUser()));
        e.addElement("password", escapeNull(this.getPswd()));
        return e;
    }

    private String escapeNull(String s) {
        return s == null ? "" : s;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    public void setDriver(int driver) {
        this.driver = driver;
    }

    public int getDriver() {
        return driver;
    }

}

public class ConnectionProfileManager {

private static List<TreeNode> profiles = new ArrayList<TreeNode>();

public static void loadProfiles() throws DocumentException{
    Element profiles = XMLUtil.readRoot(ConnectionProfileManager.class.getResourceAsStream("samples_profile.xml"));
    //Element profiles = XMLUtil.readRoot(new File(ApplicationFiles.CONNNECTION_PROFILES));
    if(profiles != null){
        for(Element profile : profiles.elements()){
            loadNode(profile, ConnectionProfileManager.profiles);
        }
    }
}


private static void loadNode(Element node, List<TreeNode> parent){
    if(node.getName().equals(XMLConstants.CP_TAG_PROFILE)){
        ConnectionProfile profile = new ConnectionProfile(node);
        parent.add(profile);
    }else if(node.getName().equals(XMLConstants.CP_TAG_FOLDER)){
        FolderNode folder = new FolderNode();
        folder.setChildren(new ArrayList<TreeNode>());
        folder.setName(node.attributeValue(XMLConstants.CP_ATTR_NAME));
        for(Element child : node.elements()){
            loadNode(child, folder.getChildren());
        }
        parent.add(folder);
    }
}

public static void saveProfiles(){
    Element root = new DefaultElement(XMLConstants.CP_TAG_PROFILES);
    for(TreeNode node : ConnectionProfileManager.profiles){
        saveNode(node, root);
    }

    XMLUtil.save(root, new File("c:\\1.xml"));
}

private static void saveNode(TreeNode node, Element root) {
    if(node instanceof ConnectionProfile){
        ConnectionProfile p = (ConnectionProfile)node;
        root.add(p.asXML());
    }else if(node instanceof FolderNode){
        FolderNode folder = (FolderNode)node;
        Element e = new DefaultElement(XMLConstants.CP_TAG_FOLDER);
        e.addAttribute(XMLConstants.CP_ATTR_NAME, node.getName());
        for(TreeNode child : folder.getChildren()){
            saveNode(child, e);
        }
        root.add(e);
    }
}


public static void addProfile(ConnectionProfile profile){
    profiles.add(profile);
}

public static void addProfile(TreeNode parentNode, ConnectionProfile profile){

}

public static List<TreeNode> getProfiles() {
    return profiles;
}

}

with these class I get my tree works, but I found It’s hard to support add operation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:21:21+00:00Added an answer on May 22, 2026 at 6:21 pm

    You’ve kinda of dictacted the answer already in the question..

    The File class is (according to the JavaDocs) an:

    abstract representation of file and directory pathnames.

    So from what you’ve described:

     // A file manager class
     class FileManager {
    
         // has a static field to hold a file collection 
         static Collection<File> fileCollection;
    
         // contains public methods such as
         public addFile(File f) { }
         public deleteFile(File f) { }
         public addFolder(File f) { }
         public deleteFolder(File f { }
     }
    

    If you have to look at implementing your own version of the File class, then the JavaDocs for that should be a good start to understanding this.

    AS to what collection is best for the file collection, I think a Set makes most sense. There’s no point having more than one file (e.g. a List and two entries of the same file would be meaningless), and testing membership of a set is a very quick operation. For example, in addFile you might check it exists before trying to add, and similarly for delete you’d want to make sure that it exists before you delete it.

    A couple of points about the design you’ve mentioned.

    1. Static fields like this as nasty. They make it difficult to test and are a pain for multi-threading. Could you make it an instance variable?

    2. Given that File is an abstract representation of a path name, why’d you need the method addFile and addFolder, they are going to have the same implementation?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code allows you to unzip file and create a folder in documents lets
I m creating document files using filemanager which is text file.i want to store
I want to open a folder window, in the appropriate file manager, from within
I do some operation on big file (around 4Mb) I do this : 1.
Basically creating a file manager. User clicks browse and a div populates with folders
I am using ubuntu, fluxbox, pcmanfm as filemanager, xmms2 as music player. My goal:
I've dealt with errors like this in Eclipse before, but I have no idea
I am working on a file manager project and can't seem to get my
I can't understand why my server isn't accepting files larger than 1MB. I am
What do I have to do to get this code work? I simply want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.