FILE IN JAVA

Maha

FILE IN JAVA

File is the most important part of any application.

Java has methods for creating, reading, updating, and deleting the files.

A file is a named location that can be used to store related information. Like main.java is a Java file that contains information about the Java program.

The File class from the java.io package, allows us to work with files.


Syntax:

import java.io.File; 

File myObj = new File("filename.txt"); 


Example:

import java.io.*;  

public class FileDemo 

{  

public static void main(String[] args) 

{  

try 

{  

File file = new File("javaFile123.txt");  

if (file.createNewFile())

{  

System.out.println("New File is created!");

else 

{  

System.out.println("File already exists.");

}  

catch (IOException e) 

{  

e.printStackTrace();  

}  

}  

}  

Output:

New File is created!

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send