Results 1 to 4 of 4
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default Fiel read suggestion needed

    Can you suggest a better way to handle this?


    Java Code:
    /**
     * This method attempts to see if any sub-directories are present and if so,
     * tries to collect and organize content of said sub-directory and create
     * (and later return) a String representation of what it found
     * 
     * @return Nicely formatted for email list of files found inside discovered
     */
    
    
    private String getError200Details() {
    	// TODO: Needs further refinement
    	StringBuffer msg = new StringBuffer();
    	String[] sourceDirContent = new File(p.getSourceFile()).list();
    		for (String element : sourceDirContent) {
    		if (new File(element).isDirectory()) {
    			String[] deepDive = new File(p.getSourceFile() + "/" + element).list();
    			for (String newelement : deepDive) {
    			msg.append("  " + Utils.whatsMyName(newelement));
    			}
    		}
    	}
    	return msg.toString();
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    yes. your code only looks two levels deep, what if there's three or more? use recursion.

    (btw, StringBuffer is so-o-o 1990-s. Use StringBuilder)

    Java Code:
    /**
     * call with the root directory, empty StringBuilder, and depth=0
     */
    private void traverseDirectory(File parent, StringBuffer sb, int depth) {
      File [] files = parent.listFiles();
      for (File child: files) {
        if (child.isDirectory) {
          traverseDirectory(new File(parent, child), sb, depth + 1);
        }
        else {
          for(int i = 0; i < depth; i ++) {
            sb.append("  ");
          }
          sb.append(child.getName());
        }
      }
    }

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    hey, but isn't StringBuilder not thread safe?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    indeed you're right. That's what makes it so much faster than StringBuffer, and when it comes to synchronization, you should really do it yourself

Similar Threads

  1. Suggestion Box In Java SE
    By V.J in forum SWT / JFace
    Replies: 4
    Last Post: 02-24-2010, 07:12 PM
  2. Need suggestion in parsing xml using sax
    By Ms.Ranjan in forum XML
    Replies: 4
    Last Post: 09-22-2009, 11:10 PM
  3. suggestion: vbcode & protocols
    By angryboy in forum Suggestions & Feedback
    Replies: 5
    Last Post: 01-28-2009, 04:43 PM
  4. THESIS PROPOSAL suggestion
    By ashin in forum New To Java
    Replies: 9
    Last Post: 11-03-2008, 01:26 PM
  5. Suggestion needed
    By java_newbie in forum NetBeans
    Replies: 5
    Last Post: 10-22-2008, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •