Maitiro Ekudzima mafaera uye maRedirector muJava

Kudzima faira muJava, tinogona kushandisa delete() nzira kubva Files kirasi. Tinogona zvakare kushandisa delete() nzira pachinhu chinova chiitiko che File kirasi.

Muenzaniso:

Kubvisa Faira Uchishandisa iyo Files kirasi

Iyo kodhi muenzaniso pazasi inoratidza maitiro ekudzima faira ne Files kirasi:

import java.io.IOException; import java.nio.file.*; public class DeleteFile {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath('./src/test/resources/newFile.txt');
try {

Files.delete(path);
} catch (NoSuchFileException x) {

System.err.format('%s: no such' + ' file or directory%n', path);
} catch (IOException x) {

System.err.println(x);
}
} }

Iyi iri pamusoro kodhi inobvisa faira rakanzi newFile.txt mu ./src/test/resources/ dhairekitori.

Iyo yakawanda catch() mabhuroko anozobata chero zvikanganiso zvakakandwa kana uchibvisa iyo faira.

Kubvisa Faira Uchishandisa iyo Faira Kirasi

Panzvimbo pekushandisa iyo delete() nzira pane Files kirasi, tinogona zvakare kushandisa delete() nzira pachinhu chinova chiitiko che File kirasi.

Muenzaniso:

import java.io.File; public class DeleteFile {
public static void main(String[] args) {
File myFile = new File('./src/test/resources/newFile.txt');
if (myFile.delete()) {

System.out.println('Deleted the file: ' + myFile.getName());
} else {

System.out.println('Failed to delete the file.');
}
} }

Delete Faira kana Iripo

Iyi inotevera kodhi inoshandisa iyo deleteIfExists() nzira usati wabvisa faira.

import java.io.IOException; import java.nio.file.*; public class DeleteFile {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath('./src/test/resources/newFile.txt');
try {

Files.deleteIfExists(path);
} catch (IOException x) {

System.err.println(x);
}
} }

Mumuenzaniso wepamusoro wekodhi, kana iyo faira isipo, iyo NoSuchFileException haina kukandwa.

Delete Directory

Tinogona kushandisa iri pamusoro kodhi kudzima dhairekitori zvakare.

Kana iyo folda isiri isina DirectoryNotEmptyException inokandwa, saka isu tinofanirwa kubata pachena kusarudzika.

import java.io.IOException; import java.nio.file.*; public class DeleteFile {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath('./src/test/resources');
try {

Files.deleteIfExists(path);
} catch (NoSuchFileException x) {

System.err.format('%s: no such' + ' file or directory%n', path);
} catch (DirectoryNotEmptyException x) {

System.err.format('%s not empty%n', path);
} catch (IOException x) {

System.err.println(x);
}
} }

Inoenderana: