java getabsolutepath_Java File getAbsolutePath()用法及代码示例

getAbsolutePath()方法是File类的一部分。该函数返回给定文件对象的绝对路径名。如果文件对象的路径名是绝对路径,那么它仅返回当前文件对象的路径。

例如:如果我们使用路径“program.txt”创建一个文件对象,则它指向存在于可执行程序所在目录中的文件(如果您使用的是IDE,它将指向保存该程序的文件)。此处上述文件的路径为“program.txt”,但该路径不是绝对路径(即不完整)。函数getAbsolutePath()将从根目录返回绝对(完整)路径。如果使用绝对路径创建文件对象,则getPath()和getAbsolutePath()将给出相同的结果。

函数签名:

public String getAbsolutePath()

函数语法:

file.getAbsolutePath()

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

返回值:该函数返回一个String值,它是给定File对象的绝对路径。

异常:如果无法访问必需的属性值,则此方法将引发Security Exception。

下面的程序将说明getAbsolutePath()方法的使用:

范例1:当前工作目录中有一个名为“program.txt”的文件。

// Java program to demonstrate the

// use of getAbsolutePath() 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");

// Get the absolute path of file f

String absolute = f.getAbsolutePath();

// Display the file path of the file object

// and also the file path of absolute file

System.out.println("Original  path: "

+ f.getPath());

System.out.println("Absolute  path: "

+ absolute);

}

catch (Exception e) {

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

}

}

}

输出:

Original Path: program.txt

Absolute Path: C:\Users\pc\eclipse-workspace1\arnab\program.txt

dff39ec73bb84e83eaf6607d8d25286b.png

范例2:当前工作目录中有一个名为“program”的目录。

// Java program to demonstrate the

// use of getAbsolutePath() function

import java.io.*;

public class solution {

public static void main(String try-catch   {

// try catch block to handle exceptions

try {

// Create a file object

File f = new File("program");

// Get the absolute path of file f

String absolute = f.getAbsolutePath();

// Display the file path of the file object

// and also the file path of absolute file

System.out.println("Original path: "

+ f.getPath());

System.out.println("Absolute path: "

+ absolute);

}

catch (Exception e) {

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

}

}

}

输出:

Original Path: program

Absolute Path: C:\Users\pc\eclipse-workspace1\arnab\program

c840666d4654bc2867b9eac00d5d48e1.png

范例3:“f:\”目录中名为“f:\program.txt”的文件。

// Java program to demonstrate the

// use of getAbsolutePath() 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("f:\\program.txt");

// get the absolute path

// of file f

String absolute = f.getAbsolutePath();

// display the file path of the file object

// and also the file path of absolute file

System.out.println("Original  path: "

+ f.getPath());

System.out.println("Absolute  path: "

+ absolute);

}

catch (Exception e) {

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

}

}

}

输出:

Original file path: f:\program.txt

Absolute file path: f:\program.txt

706e5ed83eb1cd0a2afc3052ec3a48f8.png

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用动态编译的方式将字符串转换成可执行代码。下面是一个简单的示例: ```java import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; public class StringToCode { public static void main(String[] args) throws Exception { String code = "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello World!\"); } }"; String className = "HelloWorld"; // 将字符串写入到.java文件中 File sourceFile = new File(className + ".java"); try (PrintWriter writer = new PrintWriter(new FileWriter(sourceFile))) { writer.write(code); } // 使用JavaCompiler将.java文件编译成.class文件 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, sourceFile.getAbsolutePath()); // 使用URLClassLoader加载.class文件,并执行其中的main方法 URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() }); Class<?> cls = Class.forName(className, true, classLoader); Method mainMethod = cls.getMethod("main", String[].class); mainMethod.invoke(null, new Object[] { null }); } } ``` 这个示例中,将字符串代码写入到一个名为"HelloWorld.java"的文件中,然后使用JavaCompiler进行编译,最后使用URLClassLoader加载并执行其中的main方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值