×

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 Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.