CodeGym /Blog Jawa /Acak /Carane Priksa File Ana ing Jawa
John Squirrels
tingkat
San Francisco

Carane Priksa File Ana ing Jawa

Diterbitake ing grup

Napa kita kudu mriksa yen file "ana?"

Nalika nangani operasi file (maca / nulis / nggawe / mbusak / nganyari etc), akeh wong anyar sing bisa mikir kenapa kita kudu mriksa manawa ana file? Tanggepan sing cocog kanggo iki yaiku, supaya NoSuchFileException , mesthi dadi cara sing luwih aman kanggo ngakses file. Akibate, sampeyan kudu mriksa apa file ana sadurunge ngakses kanggo ngindhari pangecualian runtime.Carane Priksa File Ana ing Jawa - 1

Carane mriksa nggunakake file.exists () cara?

Jawa menehi cara boolean prasaja, file.exists () sing ora mbutuhake paramèter kanggo mriksa file cocog ing path diwenehi. Nalika mriksa anane file, tetepake 3 skenario sing dipikirake.
  • File ditemokake.
  • File ora ditemokake.
  • Status file ora dingerteni yen ijin ora diwenehake (amarga alasan keamanan).
Metode File.exists () ngasilake " bener " yen file ditemokake. Yen ora ditemokake utawa akses gagal, bakal ngasilake " palsu ".

Tuladha

Ayo goleki conto kode sing gampang kanggo ndeleng implementasine.

package com.java.exists;
import java.io.File;

public class ExistsMethodInJava {

	public static void main(String[] args) {

		String filePath = "C:\\Users\\Lubaina\\Documents\\myNewTestFile.txt";
		File file = new File(filePath);

		// check if the file exists at the file path
		System.out.println("Does File exists at \"" + filePath + "\"?\t" + file.exists());
		
		filePath = "C:\\Users\\Lubaina\\Documents\\myOtherTestFile.txt";
		File nextFile = new File(filePath);
		
		// check if the file exists at the file path
		System.out.println("Does File exists at \"" + filePath + "\"?\t" + nextFile.exists());
	}
}
Output
Apa File ana ing "C:\Users\Lubaina\Documents\myNewTestFile.txt"? bener Apa File ana ing "C:\Users\Lubaina\Documents\myOtherTestFile.txt"? palsu
Elinga yen metode file.exists () uga bisa digunakake kanggo jalur " direktori ". Yen sampeyan mriksa path direktori sing bener nganggo metode iki, bakal ngasilake bener utawa salah. Kanggo pemahaman sing luwih apik, sampeyan bisa ndeleng blok kode ing ngisor iki.

package com.java.exists;
import java.io.File;

public class CheckFileExists {

	// check if the "file" resource exists and not "directory"
	public static boolean checkFileExists(File file) {
		return file.exists() && !file.isDirectory();
	}

	public static void main(String[] args) {

		String directoryPath = "C:\\Users\\Lubaina\\Documents\\javaContent";
		File direcotry = new File(directoryPath);

		// check if the directory exists at the dir path
		if (direcotry.exists()) {
			System.out.println("Direcotry at \"" + directoryPath + "\" exists.\n");
		} else {
			System.out.println("Direcotry at \"" + directoryPath + "\" does not exist.\n");
		}

		// check if the resource present at the path is a "file" not "directory"
		boolean check = checkFileExists(direcotry);
		System.out.println("Is the resource \"" + direcotry + "\" a File? " + check);

		String filePath = "C:\\Users\\Lubaina\\Documents\\myNewTestFile.txt";
		File file = new File(filePath);
		check = checkFileExists(file);
		System.out.println("Is the resource \"" + file + "\" a File? " + check);
	}
}
Output
Direktori ing "C:\Users\Lubaina\Documents\javaContent" ana. Apa sumber daya "C:\Users\Lubaina\Documents\javaContent" minangka File? palsu Apa sumber "C: \ Users \ Lubaina \ Documents \ myNewTestFile.txt "File? bener
Minangka sampeyan bisa ndeleng saka output, Direktori sing jenenge "javaContent" wis divalidasi kanthi metode exists () . Dadi, yen sampeyan pengin mriksa manawa file dudu direktori, sampeyan bisa nggunakake metode boolean isDirectory () sing diwenehake dening kelas File ing Jawa.

Kesimpulan

Ing pungkasan kiriman iki, sampeyan kudu ngerti carane mriksa yen file ana ing Jawa. Sampeyan bisa nulis program dhewe kanggo nyoba lan ngerti fungsi iki. Yen wis kepenak, sampeyan bisa njelajah cara liya kanggo mriksa anane File (contone, nggunakake tautan simbolis utawa kelas nio). Good luck lan seneng coding! :)

Wacan liyane:

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION