🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › dirs.html
Creating and Reading Directories (The Java™ Tutorials > Essential Java Classes > Basic I/O)
If you want to fetch only files and subdirectories where each name matches a particular pattern, you can do so by using the newDirectoryStream(Path, String) method, which provides a built-in glob filter. If you are not familiar with glob syntax, see What Is a Glob? For example, the following code snippet lists files relating to Java: .class, .java, and .jar files.: Path dir = ...; try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{java,class,jar}")) { for (Path entry: stream) { System.out.println(entry.getFileName()); } } catch (IOException x) { // IOException can never be thrown by the iteration.
🌐
Tutorialspoint
tutorialspoint.com › java › java_directories.htm
Java - Directory Operations
A directory is a File which can contain a list of other files and directories. You use File object to create directories, to list down files available in a directory. For complete detail, check a list of all the methods which you can call on File
🌐
Oracle
docs.oracle.com › javase › tutorial › jndi › concepts › directory.html
Directory Concepts (The Java™ Tutorials > Java Naming and Directory Interface > Naming and Directory Concepts)
It maps a subscriber's name to his address and phone number. A computer's directory service is very much like a telephone company's directory service in that both can be used to store information such as telephone numbers and addresses.
🌐
TechRepublic
techrepublic.com › home › developer
Directory Navigation in Java - TechRepublic
July 15, 2023 - A directory is an organizational file system structure that contains files and other directories. Java provides a few ways to traverse a directory, depending on which version you are using, including:
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java directories
Java Directories | Creation and Methods of Java Directories
March 28, 2023 - In this tutorial, we understand the concept of directories, the creation of directories in java using different methods, a listing of directories in java, creation of temporary directories, writing into the files in the directories, advantages of directories in java and programming examples to demonstrate the creation of directories in java using different methods, writing into the files in java directories along with their output snapshots. This is a guide to Java Directories.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_tjang › texts › notes-java › background › 13files_and_directories › 60directories_files.html
Java: Directories and Files
The directory name should be lowercase letters, with no blanks or other punctuation. The directory is used as the package. Put each of your classes in its own file. It's easy to do. Here are a couple of minor rules. Make sure all classes (.java files) are in the same directory (folder).
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-create-a-directory-in-java
How to Create a Directory in Java? - GeeksforGeeks
July 23, 2025 - In Java, creating a directory is a common operation when working with file system manipulation. Directories are used to organize files and other directories into a hierarchical structure.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-createdirectories-method-explained-cb824678b0b7
Java’s Files.createDirectories() Method Explained | Medium
January 26, 2025 - By converting the relative path to an absolute path using toAbsolutePath(), this code avoids confusion about where the directory will be created. Some operating systems have limits on the length of paths. On Windows, for instance, paths longer than 260 characters can cause issues unless the application is configured to use extended-length paths. import java.nio.file.*; import java.io.IOException; public class LongPathExample { public static void main(String[] args) { try { Path longPath = Paths.get("C:/very/long/path/that/exceeds/the/operating/system/limit/logs"); Files.createDirectories(longPath); } catch (IOException e) { System.err.println("Error creating directory due to path length: " + e.getMessage()); } } }
Find elsewhere
🌐
ZetCode
zetcode.com › java › listdirectory
Java list directory - how to show directory contents in Java
May 11, 2024 - Java list directory tutorial shows how to display the directory contents in Java. Directory is an organizing unit in a computer's file system for storing and locating files.
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-isdirectory-method-in-java-with-examples
File isDirectory() method in Java with Examples - GeeksforGeeks
July 11, 2025 - This function determines whether the is a file or directory denoted by the abstract filename is Directory or not.The function returns true if the abstract file path is Directory else returns false.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
Java classes, packages, and directories
Each Java class is part of a package. The first statement in a Java source file indicates which class is in what package. If the source file does not contain a package statement, the class is part of an unnamed default package.
Top answer
1 of 3
18

There is a relationship between package and directory, but it's one that you must maintain. If you have a class that's in "mypackage1.mypackage2", that means that the java command is going to expect to find it in a directory structure named "mypackage1\mypackage2" (assuming "backwards" Windows notation), with that directory structure further embedded in a directory (let's call it "myjava") whose name is in the classpath (or else is directly in the "current directory").

So your Java class (which internally says package mypackage1.mypackage2;) is in, say, "\Users\myName\myjava\mypackage1\mypackage2\", and you place "\Users\myName\myjava" in the class path, or else you have your current directory set to "\Users\myName\myjava".

If you mix this up, either the class will not be found at all, or you will get an error something like the ever-nebulous "NoClassDefFoundError".

As to why one would use packages (and directories), the reason has to do with "name space" and "separation of concerns" (look those up). Java would be much harder to keep straight if there were no packages and all the "java.lang", "java.io", "sun.misc", et al classes were together. First off, one would have to use name "prefixes" to keep them straight and avoiding name conflicts. And much of the logical grouping would be lost.

With your own projects you don't need to use packages for simple little programs you write for yourself, but if you write something you might give to someone else it's polite to use a package such as "myname.myproject" (substituting your name and project of course), so the person you give it to can combine it with others without name conflicts.

In large applications you'll find using further levels of separation helps you keep the functions straight, so you know where everything is. It also discourages you from "crossing the boundary" between different functional areas, so you don't get unrelated logic entertwined.

Eclipse (if you use that) kind of muddles the issue a bit because it "wants" to provide directory and package names and will sometimes (but not always) keep them in sync.

2 of 3
11
  • Packages provide logical namespace to your classes..

  • And these packages are stored in the form of directory levels (they are converted to nested directories) to provide physical grouping (namespace) to your classes..

Also, note that the physical namespace has to be in accordance with the logical namespace.. You can't have your class with package com.demo, under directory structure : - \com\demo\temp\, it has to be under \com\demo\, and this directory is added to the classpath so that your classes is visible to JVM when it runs your code..

Suppose you have following directory structure: -

A
|
+-- Sample.java(contains Demo class under package B)
|
+-- Outer.java(contains Demo class - no package)
|
+--B
|    |
|   +-- Demo.class
|
+--C
|    |
|   +-- Abc.class
|
+-- Demo.class

Suppose, your class Abc.class and Demo.class (under directory A), isn't define under any package, whereas your class Demo.class (under directory B) is defined under package B. So, you need to have two directories in your classpath: - \A(for two classes: - Demo.class and B.Demo.class) and \A\C(for class Abc.class)..

  • Classes under different package can use same name. That is why there won't be any conflict between the two Demo.class defined above.. Because they are in different packages. That is the whole point of dividing them into namespaces.. This is beneficial, because you will not run out of unique names for your classes..
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-display-all-the-directories-in-a-directory
Java Program to Display all the Directories in a Directory - GeeksforGeeks
July 23, 2025 - File organization structure is a hierarchical structure involving parent and child relationships just like what is there in tree data structures. It will vary from operating systems, rather the efficiency of the operating system is measured with the ease of file organization structure for the end-users. The following classes in Java are present there to list directory content
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › java_directories.htm
Java Directories - Programming Examples
Explore various Java directory examples to manage file structures and perform file operations efficiently in your Java applications.
🌐
Mkyong
mkyong.com › home › java › how to create directory in java
How to create directory in Java - Mkyong.com
July 29, 2020 - In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories. try { Path path = Paths.get("/home/mkyong/a/b/c/"); //java.nio.file.Files; Files.createDirectories(path); System.out.println("Directory is created!"); } catch (IOException e) { System.err.println("Failed to create directory!"
🌐
HappyCoders.eu
happycoders.eu › java › file-and-directory-names-file-path-paths
File and Directory Names in Java: File, Path, Paths
November 29, 2024 - In the following sections, I will not make a distinction between file and directory names. We have already seen above that, as long as the corresponding file system object does not exist, its path does not indicate whether it is a file or a directory. Instead of a constructor, we use a factory method for java.nio.file.Path to create instances.
🌐
CodeGym
codegym.cc › java blog › java developer › get the current working directory in java
Get the Current Working Directory in Java
December 26, 2024 - Getting the current working directory in Java means getting the path of the directory (folder) from where your program has launched. Normally, that means to get the path from the root folder to the folder where the program file has been placed. This is a common day to day problem...