Excluir arquivos em Java

A análise do Java Files Delete é realizada com o intuito de aprimorar o aprendizado do aluno com exemplos .



Arquivos Java Excluir

Use o método delete() do Java para remover um arquivo.

Em primeiro lugar, removemos o arquivo chamado “FirstFile.txt” que criamos e lemos nos capítulos anteriores.

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File; // Import the File class from Java.io package
public class Main{
public static void main(String[] args) {
File ample=new File("FirstFile.txt");
if(ample.delete()){
System.out.println(ample.getName()+" is successfully deleted from your system");
}
else{
System.out.println("An error is generated during the removal of this file");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

O resultado será:

Exemplos de exclusão de arquivos Java

Correspondentemente, usando o mesmo método remove(),  o arquivo chamado SecondFile.txt é removido conforme mostrado no exemplo abaixo:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File; // Import the File class from Java.io package
public class Main{
public static void main(String[] args) {
File ample=new File("SecondFile.txt");
if(ample.delete()){
System.out.println(ample.getName()+" is successfully deleted from your system");
}
else{
System.out.println("An error is generated during the removal of this file");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Excluir arquivo PDF:

Da mesma forma, também podemos excluir o arquivo .pdf , que criamos em Java Create and Read File:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File; // Import the File class from Java.io package
public class Main{
public static void main(String[] args) {
File ample=new File("NewFile.pdf");
if(ample.delete()){
System.out.println(ample.getName()+" is successfully deleted from your system");
}
else{
System.out.println("An error is generated during the removal of this file");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Excluir arquivo CSV

Da mesma forma, também podemos deletar o arquivo .csv que criamos no Java Create and Read File:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File; // Import the File class from Java.io package
public class Main{
public static void main(String[] args) {
File ample=new File("NewFile.csv");
if(ample.delete()){
System.out.println(ample.getName()+" is successfully deleted from your system");
}
else{
System.out.println("An error is generated during the removal of this file");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Excluir arquivo JSON

Também podemos deletar o arquivo .json que criamos no método Java Create and Read File da mesma forma:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File; // Import the File class from Java.io package
public class Main{
public static void main(String[] args) {
File ample=new File("NewFile.json");
if(ample.delete()){
System.out.println(ample.getName()+" is successfully deleted from your system");
}
else{
System.out.println("An error is generated during the removal of this file");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 


Excluir pasta

Uma pasta também pode ser excluída . Mas não deve haver conteúdo ou arquivos nele.

Para excluir uma pasta, não se esqueça de criá-la primeiro manualmente no diretório público (C → Usuários → Público )

Uma pasta chamada Folder1 é excluída no exemplo abaixo:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.File;
public class Main {
public static void main(String[] args) {
File mrx=new File("C:\\Users\\public\\Folder1");
if(mrx.delete()){
System.out.println("The Folder: "+mrx.getName()+" is deleted from your system");
}
else{
System.out.println("Such Directory does not Exist");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Da mesma forma, também excluímos a Pasta2 do diretório :

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.File;
public class Main {
public static void main(String[] args) {
File mrx=new File("C:\\Users\\public\\Folder2");
if(mrx.delete()){
System.out.println("The Folder: "+mrx.getName()+" is deleted from your system");
}
else{
System.out.println("Such Directory does not Exist");
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

 

Nós valorizamos o seu feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Assine a nossa newsletter
Digite seu e-mail para receber um resumo semanal de nossos melhores posts. Saber mais!
ícone