
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between the list() and listFiles() methods in Java
The class named File of the java.io package represents a file or directory (path names) in the system. To get the list of all the existing files in a directory this class provides the list() and ListFiles() methods.
The main difference between them is that
The list() method returns the names of all files in the given directory in the form of a String array.
The ListFiles() method returns the objects (File) of the files in the given directory, in the form of an array of type File.
i.e. If you just need the names of the files within a particular directory you can use the list() method and, if you need the details of the files in a directory such as name, path etc. you need to use the ListFiles() method, retrieve the objects of all the files and get the required details by invoking the respective methods.
list() method Example
import java.io.File; import java.io.IOException; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File path = new File("D:\ExampleDirectory"); //List of all files and directories String contents[] = path.list(); System.out.println("List of files and directories in the specified directory:"); for(int i=0; i < contents.length; i++) { System.out.println(contents[i]); } } }
Output
List of files and directories in the specified directory: SampleDirectory1 SampleDirectory2 SampleFile1.txt SampleFile2.txt SapmleFile3.txt
listFiles() method Example
import java.io.File; import java.io.IOException; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File path = new File("D:\ExampleDirectory"); //List of all files and directories File files [] = path.listFiles(); System.out.println("List of files and directories in the specified directory:"); for(File file : files) { System.out.println("File name: "+file.getName()); System.out.println("File path: "+file.getAbsolutePath()); System.out.println(" "); } } }
Output
List of files and directories in the specified directory: File name: SampleDirectory1 File path: D:\ExampleDirectory\SampleDirectory1 File name: SampleDirectory2 File path: D:\ExampleDirectory\SampleDirectory2 File name: SampleFile1.txt File path: D:\ExampleDirectory\SampleFile1.txt File name: SampleFile2.txt File path: D:\ExampleDirectory\SampleFile2.txt File name: SapmleFile3.txt File path: D:\ExampleDirectory\SapmleFile3.txt
- Related Articles
- Difference between Constructors and Methods in Java
- What is the difference between non-static methods and abstract methods in Java?
- Difference between List and Set in Java
- Difference Between List and ArrayList in Java
- What is the difference between getter/setter methods and constructor in Java?
- Difference between Singly linked list and Doubly linked list in Java
- Explain the difference between functions and Methods in Swift
- Difference Between add() & offer() methods in Queue in Java?
- Difference between find() and findOne() methods in MongoDB?
- Difference between shift() and pop() methods in Javascript
- Difference between push() and unshift() methods in javascript
- Difference between test () and exec () methods in Javascript
- Difference between BeforeClass and BeforeTest methods in TestNG
- What is the difference between functions and methods in JavaScript?
- What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?
