Java 中的 exists () 方法与示例


exists() 函数是 Java 中 File 类的一部分。此函数确定 是抽象文件名表示的文件或目录是否存在。如果抽象文件路径存在,则函数返回 true,否则返回 false。

语法:

public boolean exists()
file.exists()

参数:此方法不接受任何参数。

返回值:如果抽象文件名表示的文件存在,该函数将返回布尔值

例外:如果拒绝对文件的写入访问权限,此方法将引发 Security Exception

实现:考虑 file on local directory is on the system where local directory is as below provided.

// Java Program to Illustrate exists() method of File Class
//exists() 函数是 Java 中 File 类的一部分。此函数确定 是抽象文件名表示的文件或目录是否存在。如果抽象文件路径存在,则函数返回 true,否则返回 false。

//语法:

//public boolean exists()
//file.exists()
//参数:此方法不接受任何参数。

//返回值:如果抽象文件名表示的文件存在,该函数将返回布尔值。

//例外:如果拒绝对文件的写入访问权限,此方法将引发 Security Exception

// Importing input output classes
import java.io.*;

// Main class
public class GFG {

	// Main driver method
	public static void main(String args[])
	{
		// Getting the file by creating object of File class
		File f = new File("F:\\program.txt");

		// Checking if the specified file exists or not
		if (f.exists())

			// Show if the file exists
			System.out.println("Exists");
		else

			// Show if the file does not exists
			System.out.println("Does not Exists");
	}
}
//


// Java program to demonstrate
// exists() method of File Class

// Importing input output classes
import java.io.*;

// Main class
public class GFG {

	// Main driver method
	public static void main(String args[])
	{
		// Getting the file
		File f = new File("F:\\program1.txt");

		// Checking if the specified file
		// exists or not
		if (f.exists())

			// Print message if it exists
			System.out.println("Exists");
		else

			// Print message if it does not exists
			System.out.println("Does not Exists");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法资料吧!

我会继续分享编程资料,学习资料

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值