文件类字符串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 methods
import java.io.File;
// import the Exception class because it may raise an
// exception when working with files
import 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.java
D:\Programs>java GetAbsolutePath
The path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\myjava.txt
The path of the file2 if given path is not absolute : D:\Programs\java.txt

翻译自: https://www.includehelp.com/java/file-class-string-getabsolutepath-method-with-example.aspx

Java文件类字符串getAbsolutePath()方法(带示例)相关推荐

  1. java 获取文件名长度_利用Java文件类File的方法,获取磁盘文件的文件名、长度、大小等特性...

    利用Java文件类File的方法,获取磁盘文件的文件名.长度.大小等特性. 如题,大神们谁可以编写一个这样的java程序?     happy530755 | 浏览 1482 次 |举报 我 ...

  2. java getname_Java文件类字符串getName()方法(带示例)

    java getname 文件类字符串getName() (File Class String getName()) This method is available in package java. ...

  3. getparent_Java文件类字符串getParent()方法(带示例)

    getparent 文件类字符串getParent() (File Class String getParent()) This method is available in package java ...

  4. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  5. Java文件类– java.io.File

    Java File class is at the center of Java IO operations. Java File类是Java IO操作的中心. Java文件类 (Java File ...

  6. java常用类的方法,java常用类的使用方法

    java常用类的使用方法 Interger:整数类型 1.属性. static int MAX_VALUE:返回最大的整型数: static int MIN_VALUE:返回最小的整型数: stati ...

  7. Java String类的split方法简介

    Java String类的split方法简介 String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组. 1.一般用法 用一般的字符,例如 @ 或 , 等符号 ...

  8. Eclipse不自动编译java文件的终极解决方法

    Eclipse不自动编译java文件的终极解决方法 投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2015-12-13 我要评论 这篇文章主要介绍了Eclipse不自动编译java文 ...

  9. linux find批量替换java文件中字符串

    linux批量替换java文件中字符串find ./ -name '*.java' | xargs perl -pi -e 's|MoonlightL|ZXL|g' find ./ -name '*. ...

最新文章

  1. Jupyter command 'notebook': [Errno 'jupyter-notebook' not found] 2解决方法
  2. Shiro系列-Shiro如何实现身份验证
  3. fcpx调整图层_【FCPX萌新系列】新手常遇到的4个基础调色问题
  4. 萌新接触前端的第三课——JavaScript
  5. Android电池管理系统系统分析
  6. ajaxfileupload struts2 null_去掉烦人的 “ ! = null (判空语句)
  7. 偶尔出现 指定 网络名不再可用 错误提示 MS-SQL Server 基础类(尤其是在文件下载时)...
  8. Vue学习之vue-cli脚手架下载安装及配置
  9. 深度学习之文本分类总结
  10. office精英俱乐部_开放组织读书俱乐部:收回精英制
  11. Android 本地化翻译插件,解放你的双手! AndroidLocalizePlugin
  12. 同属开源Linux 移动市场MeeGo独到之秘
  13. scp 传输文件到另一台服务器
  14. 什么因素影响阿里云国际版云服务器延迟?
  15. 【FAQ】Gerrit上打开的单子无法打开,点击提示“MissingobjectException:Missing unknown xxxx”
  16. 输入十个国名 要求按字母顺序输出C语言,用C语言编程实现国家名称按序输出,要求键盘输入五个国家的名字,按字母顺序排列打印输出。...
  17. Android应用程序和其设计思想--转载----做记录
  18. pc 端与移动端区分点击与拖拽事件
  19. 前端基础之CSS标签样式
  20. Java 编写的简易画图板

热门文章

  1. linux运行core控制台程序,VisualStudioCode创建的asp.net core控制台程序部署到linux
  2. border三角形阴影(不规则图形阴影)和多重边框的制作
  3. TypeScript类型推论(Type Inference)
  4. 移动端 flexible.js 布局详解
  5. POI获取WORD信息
  6. sol - 0x60,61,62
  7. serialVersionUID的作用以及如何用idea自动生成实体类的serialVersionUID
  8. 科学计算机二进制算法,计算机是怎么理解二进制的?
  9. 计算机科学与技术专业《计算机网络原理》课程实验指导书,计算机科学导论,课程实验指导书解读.pdf...
  10. 华为服务器系统蓝屏,服务器主机蓝屏