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