Maitiro Ekutarisa kana iyo Faira kana Dhairekitori Iripo muJava

MuJava, kune nzira mbiri dzekutanga dzekutarisa kana faira kana dhairekitori riripo. Izvi ndeizvi:

1 - Files.exists kubva kuNIO package

2 - File.exists kubva kunhaka IO package

Ngatione mimwe mienzaniso kubva pasuru yega yega.

Tarisa uone kana File Riripo (Java NIO)

Iyo kodhi inoshandisa Path uye Paths kubva ku = Java NIO package) kutarisa kana faira riripo:

import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CheckFileExist {
public static void main(String[] args) {

Path path = Paths.get('/path/to/file/app.log');

if (Files.exists(path)) {

if (Files.isRegularFile(path)) {


System.out.println('App log file exists');

}

} else {

System.out.println('App log file does not exists');
}
} }

Tarisa kana Dhairekitori Iripo (Java NIO)

Saizvozvo, kana isu taida kutarisa kana dhairekitori riripo muJava uchishandisa NIO package:

import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CheckDirectoryExist {
public static void main(String[] args) {

Path path = Paths.get('/path/to/logs/');

if (Files.exists(path)) {

if (Files.isDirectory(path)) {


System.out.println('Logs directory exists');

}

} else {

System.out.println('Logs directory does not exist');
}
} }

Tarisa uone kana Faira Iripo (Java Nhaka IO)

Kana usiri kushandisa Java NIO package, unogona kushandisa iyo nhaka Java IO package.

import java.io.File; public class CheckFileExists {
public static void main(String[] args) {

File file = new File('/path/to/file/app.log');

if(file.exists()) {

System.out.println('App log file exists');
} else {

System.out.println('App log file does not exist');
}
} }

Tarisa kana Dhairekitori Iripo (Java Nhaka IO)

Saizvozvo, kutarisa dhairekitori yatinogona kushandisa:

import java.io.File; public class CheckFileExists {
public static void main(String[] args) {

File file = new File('/path/to/logs/');

if(file.isDirectory()) {

System.out.println('Logs directory exists');
} else {

System.out.println('Logs directory does not exist');
}
} }

Kuenderera mberi kuverenga











Maitiro ekuverenga mafaera muJava neMienzaniso

devqa.io










Maitiro ekudzima mafaera uye madhairekitori muJava

devqa.io