Question

how would i do subdirectory because i want to be able to grab all subdirectory out...

how would i do subdirectory because i want to be able to grab all subdirectory out of a directory and still be able to get add them


I have a subdirectory called testFolder in it contains the following: 
defaulfolder.1
defaultfolder
defaultfolder2.1
defaultfolder2

I only want to be able to grab the default folder without the "." 

i would like to get a test output of the answers


import java.io.File;
import java.util.ArrayList;
import java.util.List;



public class checkFile {

    private List<File> tests;

    public List<File> getFile(String file) {
        File f = new File(file);
        tests = new ArrayList<File>();
        if (f.isDirectory()) 
         {
            if (f.listFiles() != null)
              {
                  if(!f.getName().contains("."))
                    {
                       for (File item : f.listFiles()) {
                         tests.add(item.getName());
                      }
                   }
        } else {
            tests.add(f.getName());
        }
        return tests;
    }
}

public static void main(Stringg[] args)

{

   String test = "C:\\User\\Name\\Desktop\\testFolder

   checkFile test = new checkFile();

test.getFile(test);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class Subdirectories {
  
public static final String FOLDER_PATH = System.getProperty("user.home") // this path can be changed
+ File.separatorChar + "Desktop" // I tested on Desktop
+ File.separatorChar;
  
public static final String ROOT_FOLDER_NAME = "testFolder"; // root folder name
  
public static void main(String[] args)
{
String FINAL_FOLDER_PATH = FOLDER_PATH + ROOT_FOLDER_NAME;
  
// prints all the subdirectories under the root directory "testFolder"
System.out.println("All the directories under " + ROOT_FOLDER_NAME + ": ");
for(String folderName : listFoldersInDirectory(FINAL_FOLDER_PATH))
{
System.out.println(folderName);
}
  
// get the folder(s) without dot in their name
System.out.println("\nAll directories without dot in their name:");
for(String folderNameWithoutDot : getFolderWithoutDot(listFoldersInDirectory(FINAL_FOLDER_PATH)))
{
System.out.println(folderNameWithoutDot);
}
}
  
public static List<String> listFoldersInDirectory(String folderPath)
{
File folder = new File(folderPath);
FileFilter folderFilter = new FileFilter()
{
@Override
public boolean accept(File file) {
return file.isDirectory();
}
  
};
  
File[] foldersAsFiles = folder.listFiles(folderFilter);
List<String> folderList = new ArrayList<>(foldersAsFiles.length);
for(File folders : foldersAsFiles)
{
folderList.add(folders.getName());
}
return folderList;
}
  
public static List<String> getFolderWithoutDot(List<String> folderList)
{
List<String> foldersWithoutDot = new ArrayList<>();
for(String folderNameWithoutDot : folderList)
{
if(!folderNameWithoutDot.contains("."))
{
foldersWithoutDot.add(folderNameWithoutDot);
}
}
return foldersWithoutDot;
}
}

Add a comment
Know the answer?
Add Answer to:
how would i do subdirectory because i want to be able to grab all subdirectory out...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • How do I correct this error in my code? When it got to the file type...

    How do I correct this error in my code? When it got to the file type I can only go "null" from a certain point and I'm wondering how to correct this error. Also can I get a word document on this coding strategy for this problem? How much of this am I doing correctly? The original problem description: Create a program that provides a listing of all the files in a directory. The program should be a Java application...

  • Using listAllFiles as a guide, write a method that has one File argument: if the argument...

    Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f)   import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...

    The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter;    groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream");    System.out.println("Number of items...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT