Mar a nì thu sgrùdadh a bheil faidhle no eòlaire ann an Java

Ann an Java, tha dà phrìomh dhòigh ann air dearbhadh a bheil faidhle no eòlaire ann. Is iad sin:

1 - Files.exists bho phacaid NIO

2 - File.exists bho phacaid dìleab IO


Chì sinn cuid de na h-eisimpleirean bho gach pasgan.



Thoir sùil air a bheil File Exists (Java NIO)

Tha an còd a ’cleachdadh Path agus Paths bho phasgan Java NIO gus faighinn a-mach a bheil faidhle ann:


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');
}
} }


Thoir sùil air a bheil Directory ann (Java NIO)

San aon dòigh, nam biodh sinn airson faighinn a-mach a bheil eòlaire ann an Java a ’cleachdadh a’ phacaid NIO:



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');
}
} }


Thoir sùil air a bheil File Exists (Java Legacy IO)

Mura h-eil thu a ’cleachdadh pasgan Java NIO, faodaidh tu pasgan dìleab Java IO a chleachdadh:

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');
}
} }


Thoir sùil air a bheil Directory ann (Java Dìleab IO)

San aon dòigh, gus sgrùdadh a dhèanamh air eòlaire as urrainn dhuinn a chleachdadh:

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');
}
} }

Barrachd leughaidh












Mar a leughas tu faidhlichean ann an Java le eisimpleirean

devqa.io










Mar a sguabas tu às faidhlichean agus clàran ann an Java

devqa.io