×

Java Delete File

There are two techniques to erase a record in Java:

  1. Utilizing File.delete() technique.
  2. Utilizing File.deleteOnExit() technique.

Using File.delete() technique:

In Java, we can erase a document by utilizing the File.delete() technique for File class. The delete() technique erases the record or index meant by the theoretical pathname. On the off chance that the pathname is an index, that registry should be vacant to erase.

The strategy mark is:

public boolean delete()

The strategy returns valid in the event that the document or catalog erased effectively, in any case gets back false.

//program for understanding

import java.io.File;
public class DeleteFile1
{
//main method
public static void main(String[] args)
{
try
{
Document f= new File("E:\\demo1.txt"); //record to be erase
if(f.delete())//returns Boolean worth
{
System.out.println(f.getName() + " deleted");//getting and printing the document name
}
else
{
System.out.println("failed");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Using File.deleteOnExit() technique:

The File.deleteOnExit() technique likewise erases the document or index characterized by unique pathname.

 The deleteOnExit() strategy erases document backward request. It erases the document when JVM ends. It returns no worth. When the solicitation has been made, dropping the request is unimaginable.

So this technique ought to be utilized with care.

The technique mark is:

public void deleteOnExit()

As a rule, we utilize this technique when we need to erase the transitory document. A brief document is utilized to store the less significant and transitory information, which ought to continuously be erased when JVM ends.(JVM – Java Virtual Machine)

If we have any desire to erase the temp record physically, we can utilize File.delete() strategy.

//program for understanding

import java.io.File;
import java.io.IOException;
public class DeleteOnExitExample
{
public static void main(String[] args)
{
Record temp;
Try
{
temp = File.createTempFile("abc", ".temp");//making a .temp record
System.out.println("Temp record made at area: " + temp.getAbsolutePath());
temp.deleteOnExit();//erase record on runtime exit
System.out.println("Temp document exists : " + temp.exists());
}
get (IOException e)
{
e.printStackTrace();
}
}
}

Related Topics

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.