Me pehea te Tirohia mēnā he Kōnae, he Whaiaronga ranei kei i Java

I Java, e rua nga tikanga tuatahi mo te tirotiro mena kei te konae tetahi konae. Ko enei:

1 - Files.exists mai i te putea NIO

2 - File.exists Tuhinga ka whai mai

Me titiro e maatau etahi tauira mai i ia kohinga.

Tirohia mēnā Kei te Tīariihia te Kōnae (Java NIO)

Ka whakamahia te waehere Path me Paths mai i te kohinga Java NIO ki te tirotiro mena kei kona tetahi konae:

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

Tirohia mena kei te Rarangi Whaiaronga (Java NIO)

Waihoki, mena i hiahia matou ki te tirotiro mena kei te whai waahi tetahi raarangi ki Java ma te whakamahi i te kohinga 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');
}
} }

Tirohia mēnā kei te Tīariihia te Kōnae (Java Legacy IO)

Mena kaore koe e whakamahi i te kohinga Java NIO, ka taea e koe te whakamahi i te kohinga Java IO tawhito:

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

Tirohia mena kei te Rarangi Whaiaronga (Java Legacy IO)

Waihoki, ki te tirotiro i te raarangi ingoa ka taea e taatau te whakamahi

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

Panui atu











Me pehea te panui i nga konae i Java me nga Tauira

devqa.io










Me pehea te muku i nga konae me nga whaiaronga ki Java

devqa.io