Java
- Java Introduction
- Java Getting Started
- Java Syntax
- Java Comments
- Java Variables
- Java Data Types
- Java Type Casting
- Java Operators
- Java Strings
- Java Math
- Java Booleans
- Java If ... Else
- Java Switch
- Java While Loop
- Java For Loop
- Java Break And Continue
- Java Arrays
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Scope
- Java Recursion
- Java OOP
- Java Classes And Objects
- Java Class Attributes
- Java Class Methods
- Java Constructors
- Java Modifiers
- Java Encapsulation
- Java Packages
- Java Inheritance
- Java Polymorphism
- Java Inner Classes
- Java Abstraction
- Java Interface
- Java Enums
- Java User Input (Scanner)
- Java Date And Time
- Java ArrayList
- Java LinkedList
- Java HashMap
- Java HashSet
- Java Iterator
- Java Wrapper Classes
- Java Exceptions - Try...Catch
- Java Regular Expressions
- Java Threads
- Java Lambda Expressions
- Java Files
- Java Create And Write To Files
- Java Read Files
- Java Delete Files
Java Files
Java File Handling
File handling is an important part of any application.
Java has several methods for creating, reading, updating, and deleting files.
The File
class from the java.io
package, allows us to work with files.
To use the File
class, create an object of the class, and specify the filename or directory name:
Example
import java.io.File; // Import the File class
File myObj = new File("filename.txt"); // Specify the filename
The File
class has many useful methods for creating and getting information about files. For example:
Method | Type | Description |
---|---|---|
canRead() |
Boolean | Tests whether the file is readable or not |
canWrite() |
Boolean | Tests whether the file is writable or not |
createNewFile() |
Boolean | Creates an empty file |
delete() |
Boolean | Deletes a file |
exists() |
Boolean | Tests whether the file exists |
getName() |
String | Returns the name of the file |
getAbsolutePath() |
String | Returns the absolute pathname of the file |
length() |
Long | Returns the size of the file in bytes |
list() |
String[] | Returns an array of the files in the directory |
mkdir() |
Boolean | Creates a directory |
You will learn how to create, write, read and delete files in the next chapters:
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP