×

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

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.