博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java文件类字符串getAbsolutePath()方法(带示例)
阅读量:2534 次
发布时间:2019-05-11

本文共 3475 字,大约阅读时间需要 11 分钟。

文件类字符串getAbsolutePath() (File Class String getAbsolutePath())

  • This method is available in package java.io.File.getAbsolutePath().

    软件包java.io.File.getAbsolutePath()中提供了此方法。

  • This method is used to return the absolute path of the file object (i.e absolute path is the complete path like this c:\\ Articles\\myjava.txt) if filepath is absolute then it retrieve the complete path of the file object.

    如果filepath是绝对路径,则此方法用于返回文件对象的绝对路径(即,绝对路径是诸如c:\\ Articles \\ myjava.txt这样的完整路径),然后它将检索文件对象的完整路径。

  • The return type of this method is String so it returns the absolute path from the root in a string form.

    此方法的返回类型为String,因此它以字符串形式返回从根开始的绝对路径。

  • In this method, if we don’t give an absolute path in the file object then also it will return the absolute path of file object where your file exists.

    在这种方法中,如果我们没有在文件对象中给出绝对路径,那么它将返回文件所在的文件对象的绝对路径。

  • This method may raise an exception( i.e. Security Exception) if the desired value cannot be accessed.

    如果无法访问所需的值,则此方法可能引发异常(即Security Exception)。

Syntax:

句法:

String getAbsolutePath(){    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is String so it returns the complete path of the file object as a String.

该方法的返回类型为String,因此它以String形式返回文件对象的完整路径。

Java程序演示getAbsolutePath()方法的示例 (Java program to demonstrate example of getAbsolutePath() method)

// import the File class because we will use File class methodsimport java.io.File;// import the Exception class because it may raise an // exception when working with filesimport java.lang.Exception;public class GetAbsolutePath {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes to // escape '\' character sequence for windows otherwise // it will be considerable as url. File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt"); File file2 = new File("java.txt"); // By using getAbsolutePath() return the complete // path(whatever you have given in the file object) of the // file object because in the file1 object we already given // absolute path // [C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt] String abs_path1 = file1.getAbsolutePath(); // Display absolute path of the file object if given path is absolute. System.out.println("The path of the file 1 if given path is absolute :" + " " + abs_path1); // By using getAbsolutePath() return the complete path of the File // object even we have not given full path or absolute path is // not given [i.e.(java.txt) so it return the whole path // with filename where file exists ] String abs_path2 = file2.getAbsolutePath(); // Display absolute path of the file object if given path is not absolute. System.out.println("The path of the file2 if given path is not absolute :" + " " + abs_path2); } catch (Exception e) {
System.out.println("An error occurred."); e.printStackTrace(); } }}

Output

输出量

D:\Programs>javac GetAbsolutePath.javaD:\Programs>java GetAbsolutePathThe path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\myjava.txtThe path of the file2 if given path is not absolute : D:\Programs\java.txt

翻译自:

转载地址:http://hfazd.baihongyu.com/

你可能感兴趣的文章
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
SynchronousQueue
查看>>