java absolute_Java File isAbsolute()用法及代码示例

isAbsolute()方法是File类的一部分。该函数返回抽象路径名是否为绝对路径。

例如:如果我们使用“program.txt”路径创建文件对象,则该文件对象指向存在于可执行程序所在目录的文件(如果使用的是IDE,它将指向保存程序的文件) )。这里上面提到的文件的路径是“program.txt”,但是该路径不是绝对路径(即不完整)。绝对路径是根目录的完整路径。

函数签名:

public boolean isAbsolute()

函数语法:

file.isAbsolute()

参数:该函数不接受任何参数。

返回值:该函数返回布尔值,该值指示抽象路径名是否是绝对路径。

下面的程序将说明isAbsolute()函数的用法

范例1:给我们一个文件的文件对象,我们必须检查它是否是绝对的

// Java program to demonstrate the

// use of isAbsolute() function

import java.io.*;

public class solution {

public static void main(String args[])

{

// try-catch block to handle exceptions

try {

// Create a file object

File f = new File("c:\\users\\program.txt");

// Check if the given path

// is absolute or not

if (f.isAbsolute()) {

// Display that the path is absolute

// as the function returned true

System.out.println("The path is absolute");

}

else {

// Display that the path is not absolute

// as the function returned false

System.out.println("The path is not absolute");

}

}

catch (Exception e) {

System.err.println(e.getMessage());

}

}

}

输出:

The path is absolute

范例2:给我们一个文件的文件对象,我们必须检查它是否是绝对的

// Java program to demonstrate the

// use of isAbsolute() function

import java.io.*;

public class solution {

public static void main(String args[])

{

// try-catch block to handle exceptions

try {

// Create a file object

File f = new File("program.txt");

// Check if the given path

// is absolute or not

if (f.isAbsolute()) {

// Display that the path is absolute

// as the function returned true

System.out.println("The path is absolute");

}

else {

// Display that the path is not absolute

// as the function returned false

System.out.println("The path is not absolute");

}

}

catch (Exception e) {

System.err.println(e.getMessage());

}

}

}

输出:

The path is not absolute

这些程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的父文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
For courses in computer programming and engineering. Beginner to Intermediate Programming in Java Absolute Java provides a comprehensive reference to programming in the Java language. Accessible to both beginner and intermediate programmers, the text focuses around specifically using the Java language to practice programming techniques. The Sixth Edition is extremely flexible and easily applicable to a wide range of users. Standalone and optional chapters allow instructors to adapt the text to a variety of curse content. Highly up-to-date with new content and information regarding the use of Java, this text introduces readers to the world of programming through a widely used and relevant language. Also Available with MyProgrammingLab ™ This title is also available with MyProgrammingLab – an online homework, tutorial, and assessment program designed to work with this text to engage students and improve results. Within its structured environment, students practice what they learn, test their understanding, and pursue a personalized study plan that helps them better absorb course material and understand difficult concepts. Students, if interested in purchasing this title with MyProgrammingLab, ask your instructor for the correct package ISBN and Course ID. Instructors, contact your Pearson representative for more information. Interactive Practice helps students gain first-hand programming experience in an interactive online environment. Step-by-step VideoNote Tutorials enhance the programming concepts presented in your Pearson textbook by allowing students to view the entire problem-solving process outside of the classroom–when they need help the most. Pearson eText gives students access to their textbook anytime, anywhere. In addition to note taking, highlighting, and bookmarking, the Pearson eText offers interactive and sharing features. Rich media options let students watch lecture and example videos as they read or do their homework. Instructors can share their comments or highlights, and students can add their own, creating a tight community of learners in your class. The Pearson eText companion app allows existing subscribers to access their titles on an iPad or Android tablet for either online or offline viewing. Dynamic grading and assessment ensure your students’ submissions are automatically graded, both saving you time, and offering students immediate learning opportunities. Gradebook results can be exported to Excel to use with your LMS.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值