❶ 如何獲得當前java文件的路徑

public class Test {
public static void main(String[] args) {
String path = "Test.java";
File file = new File(path);
System.out.println(file.getAbsoluteFile());
}
}

-----
運行結果:
D:\workspaces\studyStruts2\Test.java
不加任何路徑,就是指當前路徑
望點贊

❷ JAVA如何得到文件路徑



import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

public class FileChooserDemo extends JPanel {

static final long serialVersionUID = 5854418136127725290L;

public class ExtensionFilter extends FileFilter {
private String extensions[];

private String description;

public ExtensionFilter(String description, String extension) {
this(description, new String[] { extension });
}

public ExtensionFilter(String description, String extensions[]) {
this.description = description;
this.extensions = (String[]) extensions.clone();
}

public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
int count = extensions.length;
String path = file.getAbsolutePath();
for (int i = 0; i < count; i++) {
String ext = extensions[i];
if (path.endsWith(ext)
&& (path.charAt(path.length() - ext.length()) == '.')) {
return true;
}
}
return false;
}

public String getDescription() {
return (description == null ? extensions[0] : description);
}
}

public FileChooserDemo() {
JButton jb = new JButton("Open File Viewer");
add(jb);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(".");
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileFilter type1 = new ExtensionFilter("Java source", ".java");
FileFilter type2 = new ExtensionFilter("Image files",
new String[] { ".jpg", ".gif", "jpeg", "xbm" });
FileFilter type3 = new ExtensionFilter("html files",
new String[] { ".htm", ".html" });
chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);
chooser.addChoosableFileFilter(type3);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileFilter(type2); // Initial filter setting
int status = chooser.showOpenDialog(FileChooserDemo.this);
if (status == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
System.out.println(f);
}
}
};
jb.addActionListener(listener);
}

public static void main(String args[]) {
JFrame f = new JFrame("Enhanced File Example");
JPanel j = new FileChooserDemo();
f.getContentPane().add(j, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

❸ 在java中怎麼獲得,本文件的路徑

File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以
通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:

publicclassPathTest
{
publicstaticvoidmain(String[]args)
{
Filefile=newFile(".\src\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}

getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的
路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。
getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。

下面是上面程序在我電腦上的輸出:

G:xhuojkonw.src
G:xhuojkonwsrc

❹ Java 獲取路徑的幾種方法

File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);結果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;第二種:File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;第三種:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);結果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑第四種:System.out.println(System.getProperty("user.dir"));結果:C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑第五種:System.out.println( System.getProperty("java.class.path"));結果:C:\Documents and Settings\Administrator\workspace\projectName\bin獲取當前工程路徑

❺ JAVA中如何得到文件路徑

java文件中獲得路徑
Thread.currentThread().getContextClassLoader().getResource("") //獲得資源文件(.class文件)所在路徑
ClassLoader.getSystemResource("")
Class_Name.class.getClassLoader().getResource("")
Class_Name.class .getResource("/")
Class_Name.class .getResource("") // 獲得當前類所在路徑
System.getProperty("user.dir") // 獲得項目根目錄的絕對路徑
System.getProperty("java.class.path") //得到類路徑和包路徑
列印輸出依次如下:
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/
file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/
F:\work_litao\uri_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

❻ java獲取某個文件夾的路徑怎麼寫

File類有兩個常用方法可以得到文件路徑一個是:Path(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\
G:\xhuoj\konw\src\

❼ 在java項目中如何獲取某個文件的路徑

如果是在tomcat等伺服器中運行的話,用ServletContext中的getRealPath()方法可以獲取指定文件的絕對路徑,如:getRealPath("/WEB-INF/db.xml");

❽ JAVA怎樣得到當前程序的路徑

1、利用System.getProperty()函數獲取當前路徑:

System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑

2、使用File提供的函數獲取當前路徑:

File directory = new File("");//設定為當前文件夾

try{

System.out.println(directory.getCanonicalPath());//獲取標準的路徑

System.out.println(directory.getAbsolutePath());//獲取絕對路徑

}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new
File("..")兩種路徑有所區別。

# 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾

# 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑

# 至於getPath()函數,得到的只是你在new File()時設定的路徑


Java基礎知識教程:

❾ java 怎樣獲取一個文件相對路徑

從你的代碼看,你是要在java類里獲取相對路徑 類名.class.getResourceAsStream()可以獲取到同你這個類相同內路徑下的文件 System.getProperty("user.dir")可以獲取到工容程src的路徑,下面就可以自己加路徑了嘛 提醒一點。src/com/sigls/model/filter/tt.txt";最好不要這樣寫。不同版本的JDK對這個/是有區別的,最好用 File.separator就相當於/但是在不同版本也是\所以用File.separator就可以避免這點

❿ 獲取java的.java文件路徑

TestFileInputStream.class.getResource("").getPath()+TestFileInputStream.class.getSimpleName()+".java"