Difference between revisions of "JAVA: Delete File"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Java provides methods to delete files using java programs. On the contrary to normal delete operations in any operating system, files being deleted using the java program are...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Java provides methods to delete files using java programs. On the contrary to normal delete operations in any operating system, files being deleted using the java program are deleted permanently without being moved to the trash/recycle bin.  
+
Java menyediakan method untuk menghapus file menggunakan program java. Berbeda dengan operasi penghapusan normal di sistem operasi apa pun, file yang dihapus menggunakan program java akan dihapus secara permanen tanpa dipindahkan ke tempat sampah/daur ulang.
  
Methods used to delete a file in Java:  
+
Method yang digunakan untuk men-delete file di Java, adalah:
1. Using java.io.File.delete() function: Deletes the file or directory denoted by this abstract pathname.
 
  
Syntax:  
+
==1. Menggunakan fungsi java.io.File.delete():==
 +
 
 +
Delete file atau directory yang diberikan di pathname.
 +
 
 +
Sintaks:  
  
 
  public boolean delete()
 
  public boolean delete()
  
Returns: It returns true if and only if the file or the directory is successfully deleted; false otherwise
+
Return: nilai true jika dan hanya jika file atau direktori berhasil dihapus; false jika tidak
  
 
  // Java program to delete a file
 
  // Java program to delete a file
Line 29: Line 32:
  
 
Output:  
 
Output:  
 +
File deleted successfully
 +
 +
==2. Menggunakan method java.nio.file.files.deleteifexists(Path p) yang ada di Files package:==
  
File deleted successfully
+
Method ini menghapus file jika ada. Itu juga menghapus direktori yang disebutkan di path hanya jika direktori tidak kosong.
  
2. Using java.nio.file.files.deleteifexists(Path p) method defined in Files package: This method deletes a file if it exists. It also deletes a directory mentioned in the path only if the directory is not empty.
+
Sintaks:
  
Syntax:
+
public static boolean deleteIfExists(Path path) throws IOException
  
public static boolean deleteIfExists(Path path) throws IOException
+
Parameters: path – path dari file yang akan di delete
Parameters: path – the path to the file to delete
 
  
Returns: It returns true if the file was deleted by this method; false if it could not be deleted because it did not exist.
+
Return: Mengembalikan nilai true jika file dihapus dengan metode ini; false jika tidak bisa dihapus karena tidak ada.
  
 
Throws:  
 
Throws:  
  
  DirectoryNotEmptyException – if the file is a directory and could not otherwise be deleted because the directory is not empty (optional specific exception)
+
  DirectoryNotEmptyException – jika file adalah direktori dan tidak dapat dihapus karena direktori tidak kosong (specific exception opsional)
  IOException – if an I/O error occurs.
+
  IOException – jika terjadi I/O error.
 +
 
 +
 
 
  // Java program to demonstrate delete using Files class
 
  // Java program to demonstrate delete using Files class
 
   
 
   
Line 74: Line 81:
  
 
Output:  
 
Output:  
 
+
Deletion successful.
Deletion successful.
 
  
  

Latest revision as of 05:18, 10 May 2022

Java menyediakan method untuk menghapus file menggunakan program java. Berbeda dengan operasi penghapusan normal di sistem operasi apa pun, file yang dihapus menggunakan program java akan dihapus secara permanen tanpa dipindahkan ke tempat sampah/daur ulang.

Method yang digunakan untuk men-delete file di Java, adalah:

1. Menggunakan fungsi java.io.File.delete():

Delete file atau directory yang diberikan di pathname.

Sintaks:

public boolean delete()

Return: nilai true jika dan hanya jika file atau direktori berhasil dihapus; false jika tidak

// Java program to delete a file
import java.io.*;
 
public class Test {
    public static void main(String[] args)
    {
        File file
            = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
 
        if (file.delete()) {
            System.out.println("File deleted successfully");
        }
        else {
            System.out.println("Failed to delete the file");
        }
    }
}

Output:

File deleted successfully

2. Menggunakan method java.nio.file.files.deleteifexists(Path p) yang ada di Files package:

Method ini menghapus file jika ada. Itu juga menghapus direktori yang disebutkan di path hanya jika direktori tidak kosong.

Sintaks:

public static boolean deleteIfExists(Path path) throws IOException

Parameters: path – path dari file yang akan di delete

Return: Mengembalikan nilai true jika file dihapus dengan metode ini; false jika tidak bisa dihapus karena tidak ada.

Throws:

DirectoryNotEmptyException – jika file adalah direktori dan tidak dapat dihapus karena direktori tidak kosong (specific exception opsional)
IOException – jika terjadi I/O error.


// Java program to demonstrate delete using Files class

import java.io.IOException;
import java.nio.file.*;
 
public class Test {
    public static void main(String[] args)
    {
        try {
            Files.deleteIfExists(
                Paths.get("C:\\Users\\Mayank\\Desktop\\
            445.txt"));
        }
        catch (NoSuchFileException e) {
            System.out.println(
                "No such file/directory exists");
         }
         catch (DirectoryNotEmptyException e) {
            System.out.println("Directory is not empty.");
         }
        catch (IOException e) {
            System.out.println("Invalid permissions.");
        }
 
        System.out.println("Deletion successful.");
    }
}

Output:

Deletion successful.


Referensi