Learn Pain Less

HomeOur TeamContact
Java
Working with Files, Folders java
Pawneshwer Gupta
Pawneshwer Gupta
March 25, 2015
1 min

Table Of Contents

01
Working with Files, Folders java
02
Basic operations performed by File class:
Working with Files, Folders java

Working with Files, Folders java

File and Folder operations are performed using File class in java. File class is present in ”java.io” package. You can create file or folder using File class. You can also check for file size or delete files using File class.

Basic operations performed by File class:

create folder or folders:

To create folder in java we need to initialize File object and pass path of directory in constructor parameter of File class. And then class public method of File class mkdir() if you want to create one folder or mkdirs() if you want to create nested folders.

Nested folder means if you want to create ”java/android”, android folder inside java folder and java folder not exist then mkdirs() method will first create java folder and then Android folder.

That method will return boolean result, true if this method created folder/folders successfully, else return false in case of error.

Example:

class Java{
public static void main(String[] args){
File file = new File("java/Android");
//This will create Android folder inside java folder. If folder doesn't exist.
file.mkdirs(); //this method will return Boolean result.
}
}

Check if folder/file exists:

To check whether file or folder exist, we can use .exists() method. This will return Boolean result.

Example:

class Java{
public static void main(String[] args){
File file = new File("java/Android");
boolean result = file.exists();
System.out.println(result);
}
}

Delete folder/file java:

To delete file/folder there is method called delete(), it will return Boolean result.

Example:

class Java{
public static void main(String[] args){
File f = new File("java/Android");
f.delete(); //return boolean result.
}
}

List files and folders in directory:

To get all files in any folder there is method called list(), it will return array of string type ”String[]”. This array contains name of all files and folders in that folder.

Example:

class Java{
public static void main(String[] args){
File f = new File("java");
String[] names = f.list(); //this will return files and folders name.
for(String name : names){
System.out.println(name); //print each name in array.
}
}
}

There are many other methods available for other operations. Check out them too.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

startfilesworking with filesjava tutorial

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Stream, I/O Stream in java
Stream, I/O Stream in java
March 25, 2015
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media